Merge git://git.kernel.org/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[linux-2.6/kvm.git] / fs / cifs / inode.c
blob5e8b388be3b672a7130e9e26f8bb954ed8321534
1 /*
2 * fs/cifs/inode.c
4 * Copyright (C) International Business Machines Corp., 2002,2007
5 * Author(s): Steve French (sfrench@us.ibm.com)
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/fs.h>
22 #include <linux/stat.h>
23 #include <linux/pagemap.h>
24 #include <asm/div64.h>
25 #include "cifsfs.h"
26 #include "cifspdu.h"
27 #include "cifsglob.h"
28 #include "cifsproto.h"
29 #include "cifs_debug.h"
30 #include "cifs_fs_sb.h"
32 int cifs_get_inode_info_unix(struct inode **pinode,
33 const unsigned char *search_path, struct super_block *sb, int xid)
35 int rc = 0;
36 FILE_UNIX_BASIC_INFO findData;
37 struct cifsTconInfo *pTcon;
38 struct inode *inode;
39 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
40 char *tmp_path;
42 pTcon = cifs_sb->tcon;
43 cFYI(1, ("Getting info on %s", search_path));
44 /* could have done a find first instead but this returns more info */
45 rc = CIFSSMBUnixQPathInfo(xid, pTcon, search_path, &findData,
46 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
47 CIFS_MOUNT_MAP_SPECIAL_CHR);
48 /* dump_mem("\nUnixQPathInfo return data", &findData,
49 sizeof(findData)); */
50 if (rc) {
51 if (rc == -EREMOTE) {
52 tmp_path =
53 kmalloc(strnlen(pTcon->treeName,
54 MAX_TREE_SIZE + 1) +
55 strnlen(search_path, MAX_PATHCONF) + 1,
56 GFP_KERNEL);
57 if (tmp_path == NULL) {
58 return -ENOMEM;
60 /* have to skip first of the double backslash of
61 UNC name */
62 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
63 strncat(tmp_path, search_path, MAX_PATHCONF);
64 rc = connect_to_dfs_path(xid, pTcon->ses,
65 /* treename + */ tmp_path,
66 cifs_sb->local_nls,
67 cifs_sb->mnt_cifs_flags &
68 CIFS_MOUNT_MAP_SPECIAL_CHR);
69 kfree(tmp_path);
71 /* BB fix up inode etc. */
72 } else if (rc) {
73 return rc;
75 } else {
76 struct cifsInodeInfo *cifsInfo;
77 __u32 type = le32_to_cpu(findData.Type);
78 __u64 num_of_bytes = le64_to_cpu(findData.NumOfBytes);
79 __u64 end_of_file = le64_to_cpu(findData.EndOfFile);
81 /* get new inode */
82 if (*pinode == NULL) {
83 *pinode = new_inode(sb);
84 if (*pinode == NULL)
85 return -ENOMEM;
86 /* Is an i_ino of zero legal? */
87 /* Are there sanity checks we can use to ensure that
88 the server is really filling in that field? */
89 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
90 (*pinode)->i_ino =
91 (unsigned long)findData.UniqueId;
92 } /* note ino incremented to unique num in new_inode */
93 if (sb->s_flags & MS_NOATIME)
94 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
96 insert_inode_hash(*pinode);
99 inode = *pinode;
100 cifsInfo = CIFS_I(inode);
102 cFYI(1, ("Old time %ld", cifsInfo->time));
103 cifsInfo->time = jiffies;
104 cFYI(1, ("New time %ld", cifsInfo->time));
105 /* this is ok to set on every inode revalidate */
106 atomic_set(&cifsInfo->inUse, 1);
108 inode->i_atime =
109 cifs_NTtimeToUnix(le64_to_cpu(findData.LastAccessTime));
110 inode->i_mtime =
111 cifs_NTtimeToUnix(le64_to_cpu
112 (findData.LastModificationTime));
113 inode->i_ctime =
114 cifs_NTtimeToUnix(le64_to_cpu(findData.LastStatusChange));
115 inode->i_mode = le64_to_cpu(findData.Permissions);
116 /* since we set the inode type below we need to mask off
117 to avoid strange results if bits set above */
118 inode->i_mode &= ~S_IFMT;
119 if (type == UNIX_FILE) {
120 inode->i_mode |= S_IFREG;
121 } else if (type == UNIX_SYMLINK) {
122 inode->i_mode |= S_IFLNK;
123 } else if (type == UNIX_DIR) {
124 inode->i_mode |= S_IFDIR;
125 } else if (type == UNIX_CHARDEV) {
126 inode->i_mode |= S_IFCHR;
127 inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
128 le64_to_cpu(findData.DevMinor) & MINORMASK);
129 } else if (type == UNIX_BLOCKDEV) {
130 inode->i_mode |= S_IFBLK;
131 inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
132 le64_to_cpu(findData.DevMinor) & MINORMASK);
133 } else if (type == UNIX_FIFO) {
134 inode->i_mode |= S_IFIFO;
135 } else if (type == UNIX_SOCKET) {
136 inode->i_mode |= S_IFSOCK;
137 } else {
138 /* safest to call it a file if we do not know */
139 inode->i_mode |= S_IFREG;
140 cFYI(1, ("unknown type %d", type));
143 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
144 inode->i_uid = cifs_sb->mnt_uid;
145 else
146 inode->i_uid = le64_to_cpu(findData.Uid);
148 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
149 inode->i_gid = cifs_sb->mnt_gid;
150 else
151 inode->i_gid = le64_to_cpu(findData.Gid);
153 inode->i_nlink = le64_to_cpu(findData.Nlinks);
155 spin_lock(&inode->i_lock);
156 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
157 /* can not safely change the file size here if the
158 client is writing to it due to potential races */
159 i_size_write(inode, end_of_file);
161 /* blksize needs to be multiple of two. So safer to default to
162 blksize and blkbits set in superblock so 2**blkbits and blksize
163 will match rather than setting to:
164 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
166 /* This seems incredibly stupid but it turns out that i_blocks
167 is not related to (i_size / i_blksize), instead 512 byte size
168 is required for calculating num blocks */
170 /* 512 bytes (2**9) is the fake blocksize that must be used */
171 /* for this calculation */
172 inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
174 spin_unlock(&inode->i_lock);
176 if (num_of_bytes < end_of_file)
177 cFYI(1, ("allocation size less than end of file"));
178 cFYI(1, ("Size %ld and blocks %llu",
179 (unsigned long) inode->i_size,
180 (unsigned long long)inode->i_blocks));
181 if (S_ISREG(inode->i_mode)) {
182 cFYI(1, ("File inode"));
183 inode->i_op = &cifs_file_inode_ops;
184 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
185 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
186 inode->i_fop =
187 &cifs_file_direct_nobrl_ops;
188 else
189 inode->i_fop = &cifs_file_direct_ops;
190 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
191 inode->i_fop = &cifs_file_nobrl_ops;
192 else /* not direct, send byte range locks */
193 inode->i_fop = &cifs_file_ops;
195 /* check if server can support readpages */
196 if (pTcon->ses->server->maxBuf <
197 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
198 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
199 else
200 inode->i_data.a_ops = &cifs_addr_ops;
201 } else if (S_ISDIR(inode->i_mode)) {
202 cFYI(1, ("Directory inode"));
203 inode->i_op = &cifs_dir_inode_ops;
204 inode->i_fop = &cifs_dir_ops;
205 } else if (S_ISLNK(inode->i_mode)) {
206 cFYI(1, ("Symbolic Link inode"));
207 inode->i_op = &cifs_symlink_inode_ops;
208 /* tmp_inode->i_fop = */ /* do not need to set to anything */
209 } else {
210 cFYI(1, ("Init special inode"));
211 init_special_inode(inode, inode->i_mode,
212 inode->i_rdev);
215 return rc;
218 static int decode_sfu_inode(struct inode *inode, __u64 size,
219 const unsigned char *path,
220 struct cifs_sb_info *cifs_sb, int xid)
222 int rc;
223 int oplock = FALSE;
224 __u16 netfid;
225 struct cifsTconInfo *pTcon = cifs_sb->tcon;
226 char buf[24];
227 unsigned int bytes_read;
228 char *pbuf;
230 pbuf = buf;
232 if (size == 0) {
233 inode->i_mode |= S_IFIFO;
234 return 0;
235 } else if (size < 8) {
236 return -EINVAL; /* EOPNOTSUPP? */
239 rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
240 CREATE_NOT_DIR, &netfid, &oplock, NULL,
241 cifs_sb->local_nls,
242 cifs_sb->mnt_cifs_flags &
243 CIFS_MOUNT_MAP_SPECIAL_CHR);
244 if (rc == 0) {
245 int buf_type = CIFS_NO_BUFFER;
246 /* Read header */
247 rc = CIFSSMBRead(xid, pTcon,
248 netfid,
249 24 /* length */, 0 /* offset */,
250 &bytes_read, &pbuf, &buf_type);
251 if ((rc == 0) && (bytes_read >= 8)) {
252 if (memcmp("IntxBLK", pbuf, 8) == 0) {
253 cFYI(1, ("Block device"));
254 inode->i_mode |= S_IFBLK;
255 if (bytes_read == 24) {
256 /* we have enough to decode dev num */
257 __u64 mjr; /* major */
258 __u64 mnr; /* minor */
259 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
260 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
261 inode->i_rdev = MKDEV(mjr, mnr);
263 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
264 cFYI(1, ("Char device"));
265 inode->i_mode |= S_IFCHR;
266 if (bytes_read == 24) {
267 /* we have enough to decode dev num */
268 __u64 mjr; /* major */
269 __u64 mnr; /* minor */
270 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
271 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
272 inode->i_rdev = MKDEV(mjr, mnr);
274 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
275 cFYI(1, ("Symlink"));
276 inode->i_mode |= S_IFLNK;
277 } else {
278 inode->i_mode |= S_IFREG; /* file? */
279 rc = -EOPNOTSUPP;
281 } else {
282 inode->i_mode |= S_IFREG; /* then it is a file */
283 rc = -EOPNOTSUPP; /* or some unknown SFU type */
285 CIFSSMBClose(xid, pTcon, netfid);
287 return rc;
290 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
292 static int get_sfu_uid_mode(struct inode *inode,
293 const unsigned char *path,
294 struct cifs_sb_info *cifs_sb, int xid)
296 #ifdef CONFIG_CIFS_XATTR
297 ssize_t rc;
298 char ea_value[4];
299 __u32 mode;
301 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
302 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
303 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
304 if (rc < 0)
305 return (int)rc;
306 else if (rc > 3) {
307 mode = le32_to_cpu(*((__le32 *)ea_value));
308 inode->i_mode &= ~SFBITS_MASK;
309 cFYI(1, ("special bits 0%o org mode 0%o", mode, inode->i_mode));
310 inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
311 cFYI(1, ("special mode bits 0%o", mode));
312 return 0;
313 } else {
314 return 0;
316 #else
317 return -EOPNOTSUPP;
318 #endif
321 int cifs_get_inode_info(struct inode **pinode,
322 const unsigned char *search_path, FILE_ALL_INFO *pfindData,
323 struct super_block *sb, int xid)
325 int rc = 0;
326 struct cifsTconInfo *pTcon;
327 struct inode *inode;
328 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
329 char *tmp_path;
330 char *buf = NULL;
331 int adjustTZ = FALSE;
333 pTcon = cifs_sb->tcon;
334 cFYI(1, ("Getting info on %s", search_path));
336 if ((pfindData == NULL) && (*pinode != NULL)) {
337 if (CIFS_I(*pinode)->clientCanCacheRead) {
338 cFYI(1, ("No need to revalidate cached inode sizes"));
339 return rc;
343 /* if file info not passed in then get it from server */
344 if (pfindData == NULL) {
345 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
346 if (buf == NULL)
347 return -ENOMEM;
348 pfindData = (FILE_ALL_INFO *)buf;
349 /* could do find first instead but this returns more info */
350 rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
351 0 /* not legacy */,
352 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
353 CIFS_MOUNT_MAP_SPECIAL_CHR);
354 /* BB optimize code so we do not make the above call
355 when server claims no NT SMB support and the above call
356 failed at least once - set flag in tcon or mount */
357 if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
358 rc = SMBQueryInformation(xid, pTcon, search_path,
359 pfindData, cifs_sb->local_nls,
360 cifs_sb->mnt_cifs_flags &
361 CIFS_MOUNT_MAP_SPECIAL_CHR);
362 adjustTZ = TRUE;
365 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
366 if (rc) {
367 if (rc == -EREMOTE) {
368 tmp_path =
369 kmalloc(strnlen
370 (pTcon->treeName,
371 MAX_TREE_SIZE + 1) +
372 strnlen(search_path, MAX_PATHCONF) + 1,
373 GFP_KERNEL);
374 if (tmp_path == NULL) {
375 kfree(buf);
376 return -ENOMEM;
379 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
380 strncat(tmp_path, search_path, MAX_PATHCONF);
381 rc = connect_to_dfs_path(xid, pTcon->ses,
382 /* treename + */ tmp_path,
383 cifs_sb->local_nls,
384 cifs_sb->mnt_cifs_flags &
385 CIFS_MOUNT_MAP_SPECIAL_CHR);
386 kfree(tmp_path);
387 /* BB fix up inode etc. */
388 } else if (rc) {
389 kfree(buf);
390 return rc;
392 } else {
393 struct cifsInodeInfo *cifsInfo;
394 __u32 attr = le32_to_cpu(pfindData->Attributes);
396 /* get new inode */
397 if (*pinode == NULL) {
398 *pinode = new_inode(sb);
399 if (*pinode == NULL) {
400 kfree(buf);
401 return -ENOMEM;
403 /* Is an i_ino of zero legal? Can we use that to check
404 if the server supports returning inode numbers? Are
405 there other sanity checks we can use to ensure that
406 the server is really filling in that field? */
408 /* We can not use the IndexNumber field by default from
409 Windows or Samba (in ALL_INFO buf) but we can request
410 it explicitly. It may not be unique presumably if
411 the server has multiple devices mounted under one
412 share */
414 /* There may be higher info levels that work but are
415 there Windows server or network appliances for which
416 IndexNumber field is not guaranteed unique? */
418 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
419 int rc1 = 0;
420 __u64 inode_num;
422 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
423 search_path, &inode_num,
424 cifs_sb->local_nls,
425 cifs_sb->mnt_cifs_flags &
426 CIFS_MOUNT_MAP_SPECIAL_CHR);
427 if (rc1) {
428 cFYI(1, ("GetSrvInodeNum rc %d", rc1));
429 /* BB EOPNOSUPP disable SERVER_INUM? */
430 } else /* do we need cast or hash to ino? */
431 (*pinode)->i_ino = inode_num;
432 } /* else ino incremented to unique num in new_inode*/
433 if (sb->s_flags & MS_NOATIME)
434 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
435 insert_inode_hash(*pinode);
437 inode = *pinode;
438 cifsInfo = CIFS_I(inode);
439 cifsInfo->cifsAttrs = attr;
440 cFYI(1, ("Old time %ld", cifsInfo->time));
441 cifsInfo->time = jiffies;
442 cFYI(1, ("New time %ld", cifsInfo->time));
444 /* blksize needs to be multiple of two. So safer to default to
445 blksize and blkbits set in superblock so 2**blkbits and blksize
446 will match rather than setting to:
447 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
449 /* Linux can not store file creation time so ignore it */
450 if (pfindData->LastAccessTime)
451 inode->i_atime = cifs_NTtimeToUnix
452 (le64_to_cpu(pfindData->LastAccessTime));
453 else /* do not need to use current_fs_time - time not stored */
454 inode->i_atime = CURRENT_TIME;
455 inode->i_mtime =
456 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
457 inode->i_ctime =
458 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
459 cFYI(0, ("Attributes came in as 0x%x", attr));
460 if (adjustTZ && (pTcon->ses) && (pTcon->ses->server)) {
461 inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj;
462 inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj;
465 /* set default mode. will override for dirs below */
466 if (atomic_read(&cifsInfo->inUse) == 0)
467 /* new inode, can safely set these fields */
468 inode->i_mode = cifs_sb->mnt_file_mode;
469 else /* since we set the inode type below we need to mask off
470 to avoid strange results if type changes and both
471 get orred in */
472 inode->i_mode &= ~S_IFMT;
473 /* if (attr & ATTR_REPARSE) */
474 /* We no longer handle these as symlinks because we could not
475 follow them due to the absolute path with drive letter */
476 if (attr & ATTR_DIRECTORY) {
477 /* override default perms since we do not do byte range locking
478 on dirs */
479 inode->i_mode = cifs_sb->mnt_dir_mode;
480 inode->i_mode |= S_IFDIR;
481 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
482 (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
483 /* No need to le64 convert size of zero */
484 (pfindData->EndOfFile == 0)) {
485 inode->i_mode = cifs_sb->mnt_file_mode;
486 inode->i_mode |= S_IFIFO;
487 /* BB Finish for SFU style symlinks and devices */
488 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
489 (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
490 if (decode_sfu_inode(inode,
491 le64_to_cpu(pfindData->EndOfFile),
492 search_path,
493 cifs_sb, xid)) {
494 cFYI(1, ("Unrecognized sfu inode type"));
496 cFYI(1, ("sfu mode 0%o", inode->i_mode));
497 } else {
498 inode->i_mode |= S_IFREG;
499 /* treat the dos attribute of read-only as read-only
500 mode e.g. 555 */
501 if (cifsInfo->cifsAttrs & ATTR_READONLY)
502 inode->i_mode &= ~(S_IWUGO);
503 else if ((inode->i_mode & S_IWUGO) == 0)
504 /* the ATTR_READONLY flag may have been */
505 /* changed on server -- set any w bits */
506 /* allowed by mnt_file_mode */
507 inode->i_mode |= (S_IWUGO &
508 cifs_sb->mnt_file_mode);
509 /* BB add code here -
510 validate if device or weird share or device type? */
513 spin_lock(&inode->i_lock);
514 if (is_size_safe_to_change(cifsInfo, le64_to_cpu(pfindData->EndOfFile))) {
515 /* can not safely shrink the file size here if the
516 client is writing to it due to potential races */
517 i_size_write(inode, le64_to_cpu(pfindData->EndOfFile));
519 /* 512 bytes (2**9) is the fake blocksize that must be
520 used for this calculation */
521 inode->i_blocks = (512 - 1 + le64_to_cpu(
522 pfindData->AllocationSize)) >> 9;
524 spin_unlock(&inode->i_lock);
526 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
528 /* BB fill in uid and gid here? with help from winbind?
529 or retrieve from NTFS stream extended attribute */
530 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
531 /* fill in uid, gid, mode from server ACL */
532 /* BB FIXME this should also take into account the
533 * default uid specified on mount if present */
534 get_sfu_uid_mode(inode, search_path, cifs_sb, xid);
535 } else if (atomic_read(&cifsInfo->inUse) == 0) {
536 inode->i_uid = cifs_sb->mnt_uid;
537 inode->i_gid = cifs_sb->mnt_gid;
538 /* set so we do not keep refreshing these fields with
539 bad data after user has changed them in memory */
540 atomic_set(&cifsInfo->inUse, 1);
543 if (S_ISREG(inode->i_mode)) {
544 cFYI(1, ("File inode"));
545 inode->i_op = &cifs_file_inode_ops;
546 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
547 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
548 inode->i_fop =
549 &cifs_file_direct_nobrl_ops;
550 else
551 inode->i_fop = &cifs_file_direct_ops;
552 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
553 inode->i_fop = &cifs_file_nobrl_ops;
554 else /* not direct, send byte range locks */
555 inode->i_fop = &cifs_file_ops;
557 if (pTcon->ses->server->maxBuf <
558 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
559 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
560 else
561 inode->i_data.a_ops = &cifs_addr_ops;
562 } else if (S_ISDIR(inode->i_mode)) {
563 cFYI(1, ("Directory inode"));
564 inode->i_op = &cifs_dir_inode_ops;
565 inode->i_fop = &cifs_dir_ops;
566 } else if (S_ISLNK(inode->i_mode)) {
567 cFYI(1, ("Symbolic Link inode"));
568 inode->i_op = &cifs_symlink_inode_ops;
569 } else {
570 init_special_inode(inode, inode->i_mode,
571 inode->i_rdev);
574 kfree(buf);
575 return rc;
578 static const struct inode_operations cifs_ipc_inode_ops = {
579 .lookup = cifs_lookup,
582 /* gets root inode */
583 void cifs_read_inode(struct inode *inode)
585 int xid, rc;
586 struct cifs_sb_info *cifs_sb;
588 cifs_sb = CIFS_SB(inode->i_sb);
589 xid = GetXid();
591 if (cifs_sb->tcon->unix_ext)
592 rc = cifs_get_inode_info_unix(&inode, "", inode->i_sb, xid);
593 else
594 rc = cifs_get_inode_info(&inode, "", NULL, inode->i_sb, xid);
595 if (rc && cifs_sb->tcon->ipc) {
596 cFYI(1, ("ipc connection - fake read inode"));
597 inode->i_mode |= S_IFDIR;
598 inode->i_nlink = 2;
599 inode->i_op = &cifs_ipc_inode_ops;
600 inode->i_fop = &simple_dir_operations;
601 inode->i_uid = cifs_sb->mnt_uid;
602 inode->i_gid = cifs_sb->mnt_gid;
605 /* can not call macro FreeXid here since in a void func */
606 _FreeXid(xid);
609 int cifs_unlink(struct inode *inode, struct dentry *direntry)
611 int rc = 0;
612 int xid;
613 struct cifs_sb_info *cifs_sb;
614 struct cifsTconInfo *pTcon;
615 char *full_path = NULL;
616 struct cifsInodeInfo *cifsInode;
617 FILE_BASIC_INFO *pinfo_buf;
619 cFYI(1, ("cifs_unlink, inode = 0x%p", inode));
621 xid = GetXid();
623 if (inode)
624 cifs_sb = CIFS_SB(inode->i_sb);
625 else
626 cifs_sb = CIFS_SB(direntry->d_sb);
627 pTcon = cifs_sb->tcon;
629 /* Unlink can be called from rename so we can not grab the sem here
630 since we deadlock otherwise */
631 /* mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);*/
632 full_path = build_path_from_dentry(direntry);
633 /* mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);*/
634 if (full_path == NULL) {
635 FreeXid(xid);
636 return -ENOMEM;
639 if ((pTcon->ses->capabilities & CAP_UNIX) &&
640 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
641 le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
642 rc = CIFSPOSIXDelFile(xid, pTcon, full_path,
643 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
644 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
645 cFYI(1, ("posix del rc %d", rc));
646 if ((rc == 0) || (rc == -ENOENT))
647 goto psx_del_no_retry;
650 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
651 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
652 psx_del_no_retry:
653 if (!rc) {
654 if (direntry->d_inode)
655 drop_nlink(direntry->d_inode);
656 } else if (rc == -ENOENT) {
657 d_drop(direntry);
658 } else if (rc == -ETXTBSY) {
659 int oplock = FALSE;
660 __u16 netfid;
662 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
663 CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
664 &netfid, &oplock, NULL, cifs_sb->local_nls,
665 cifs_sb->mnt_cifs_flags &
666 CIFS_MOUNT_MAP_SPECIAL_CHR);
667 if (rc == 0) {
668 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
669 cifs_sb->local_nls,
670 cifs_sb->mnt_cifs_flags &
671 CIFS_MOUNT_MAP_SPECIAL_CHR);
672 CIFSSMBClose(xid, pTcon, netfid);
673 if (direntry->d_inode)
674 drop_nlink(direntry->d_inode);
676 } else if (rc == -EACCES) {
677 /* try only if r/o attribute set in local lookup data? */
678 pinfo_buf = kzalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
679 if (pinfo_buf) {
680 /* ATTRS set to normal clears r/o bit */
681 pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
682 if (!(pTcon->ses->flags & CIFS_SES_NT4))
683 rc = CIFSSMBSetTimes(xid, pTcon, full_path,
684 pinfo_buf,
685 cifs_sb->local_nls,
686 cifs_sb->mnt_cifs_flags &
687 CIFS_MOUNT_MAP_SPECIAL_CHR);
688 else
689 rc = -EOPNOTSUPP;
691 if (rc == -EOPNOTSUPP) {
692 int oplock = FALSE;
693 __u16 netfid;
694 /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
695 full_path,
696 (__u16)ATTR_NORMAL,
697 cifs_sb->local_nls);
698 For some strange reason it seems that NT4 eats the
699 old setattr call without actually setting the
700 attributes so on to the third attempted workaround
703 /* BB could scan to see if we already have it open
704 and pass in pid of opener to function */
705 rc = CIFSSMBOpen(xid, pTcon, full_path,
706 FILE_OPEN, SYNCHRONIZE |
707 FILE_WRITE_ATTRIBUTES, 0,
708 &netfid, &oplock, NULL,
709 cifs_sb->local_nls,
710 cifs_sb->mnt_cifs_flags &
711 CIFS_MOUNT_MAP_SPECIAL_CHR);
712 if (rc == 0) {
713 rc = CIFSSMBSetFileTimes(xid, pTcon,
714 pinfo_buf,
715 netfid);
716 CIFSSMBClose(xid, pTcon, netfid);
719 kfree(pinfo_buf);
721 if (rc == 0) {
722 rc = CIFSSMBDelFile(xid, pTcon, full_path,
723 cifs_sb->local_nls,
724 cifs_sb->mnt_cifs_flags &
725 CIFS_MOUNT_MAP_SPECIAL_CHR);
726 if (!rc) {
727 if (direntry->d_inode)
728 drop_nlink(direntry->d_inode);
729 } else if (rc == -ETXTBSY) {
730 int oplock = FALSE;
731 __u16 netfid;
733 rc = CIFSSMBOpen(xid, pTcon, full_path,
734 FILE_OPEN, DELETE,
735 CREATE_NOT_DIR |
736 CREATE_DELETE_ON_CLOSE,
737 &netfid, &oplock, NULL,
738 cifs_sb->local_nls,
739 cifs_sb->mnt_cifs_flags &
740 CIFS_MOUNT_MAP_SPECIAL_CHR);
741 if (rc == 0) {
742 CIFSSMBRenameOpenFile(xid, pTcon,
743 netfid, NULL,
744 cifs_sb->local_nls,
745 cifs_sb->mnt_cifs_flags &
746 CIFS_MOUNT_MAP_SPECIAL_CHR);
747 CIFSSMBClose(xid, pTcon, netfid);
748 if (direntry->d_inode)
749 drop_nlink(direntry->d_inode);
751 /* BB if rc = -ETXTBUSY goto the rename logic BB */
755 if (direntry->d_inode) {
756 cifsInode = CIFS_I(direntry->d_inode);
757 cifsInode->time = 0; /* will force revalidate to get info
758 when needed */
759 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
761 if (inode) {
762 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
763 cifsInode = CIFS_I(inode);
764 cifsInode->time = 0; /* force revalidate of dir as well */
767 kfree(full_path);
768 FreeXid(xid);
769 return rc;
772 static void posix_fill_in_inode(struct inode *tmp_inode,
773 FILE_UNIX_BASIC_INFO *pData, int *pobject_type, int isNewInode)
775 loff_t local_size;
776 struct timespec local_mtime;
778 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
779 struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
781 __u32 type = le32_to_cpu(pData->Type);
782 __u64 num_of_bytes = le64_to_cpu(pData->NumOfBytes);
783 __u64 end_of_file = le64_to_cpu(pData->EndOfFile);
784 cifsInfo->time = jiffies;
785 atomic_inc(&cifsInfo->inUse);
787 /* save mtime and size */
788 local_mtime = tmp_inode->i_mtime;
789 local_size = tmp_inode->i_size;
791 tmp_inode->i_atime =
792 cifs_NTtimeToUnix(le64_to_cpu(pData->LastAccessTime));
793 tmp_inode->i_mtime =
794 cifs_NTtimeToUnix(le64_to_cpu(pData->LastModificationTime));
795 tmp_inode->i_ctime =
796 cifs_NTtimeToUnix(le64_to_cpu(pData->LastStatusChange));
798 tmp_inode->i_mode = le64_to_cpu(pData->Permissions);
799 /* since we set the inode type below we need to mask off type
800 to avoid strange results if bits above were corrupt */
801 tmp_inode->i_mode &= ~S_IFMT;
802 if (type == UNIX_FILE) {
803 *pobject_type = DT_REG;
804 tmp_inode->i_mode |= S_IFREG;
805 } else if (type == UNIX_SYMLINK) {
806 *pobject_type = DT_LNK;
807 tmp_inode->i_mode |= S_IFLNK;
808 } else if (type == UNIX_DIR) {
809 *pobject_type = DT_DIR;
810 tmp_inode->i_mode |= S_IFDIR;
811 } else if (type == UNIX_CHARDEV) {
812 *pobject_type = DT_CHR;
813 tmp_inode->i_mode |= S_IFCHR;
814 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pData->DevMajor),
815 le64_to_cpu(pData->DevMinor) & MINORMASK);
816 } else if (type == UNIX_BLOCKDEV) {
817 *pobject_type = DT_BLK;
818 tmp_inode->i_mode |= S_IFBLK;
819 tmp_inode->i_rdev = MKDEV(le64_to_cpu(pData->DevMajor),
820 le64_to_cpu(pData->DevMinor) & MINORMASK);
821 } else if (type == UNIX_FIFO) {
822 *pobject_type = DT_FIFO;
823 tmp_inode->i_mode |= S_IFIFO;
824 } else if (type == UNIX_SOCKET) {
825 *pobject_type = DT_SOCK;
826 tmp_inode->i_mode |= S_IFSOCK;
827 } else {
828 /* safest to just call it a file */
829 *pobject_type = DT_REG;
830 tmp_inode->i_mode |= S_IFREG;
831 cFYI(1, ("unknown inode type %d", type));
834 #ifdef CONFIG_CIFS_DEBUG2
835 cFYI(1, ("object type: %d", type));
836 #endif
837 tmp_inode->i_uid = le64_to_cpu(pData->Uid);
838 tmp_inode->i_gid = le64_to_cpu(pData->Gid);
839 tmp_inode->i_nlink = le64_to_cpu(pData->Nlinks);
841 spin_lock(&tmp_inode->i_lock);
842 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
843 /* can not safely change the file size here if the
844 client is writing to it due to potential races */
845 i_size_write(tmp_inode, end_of_file);
847 /* 512 bytes (2**9) is the fake blocksize that must be used */
848 /* for this calculation, not the real blocksize */
849 tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
851 spin_unlock(&tmp_inode->i_lock);
853 if (S_ISREG(tmp_inode->i_mode)) {
854 cFYI(1, ("File inode"));
855 tmp_inode->i_op = &cifs_file_inode_ops;
857 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
858 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
859 tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
860 else
861 tmp_inode->i_fop = &cifs_file_direct_ops;
863 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
864 tmp_inode->i_fop = &cifs_file_nobrl_ops;
865 else
866 tmp_inode->i_fop = &cifs_file_ops;
868 if ((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
869 (cifs_sb->tcon->ses->server->maxBuf <
870 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE))
871 tmp_inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
872 else
873 tmp_inode->i_data.a_ops = &cifs_addr_ops;
875 if (isNewInode)
876 return; /* No sense invalidating pages for new inode
877 since we we have not started caching
878 readahead file data yet */
880 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
881 (local_size == tmp_inode->i_size)) {
882 cFYI(1, ("inode exists but unchanged"));
883 } else {
884 /* file may have changed on server */
885 cFYI(1, ("invalidate inode, readdir detected change"));
886 invalidate_remote_inode(tmp_inode);
888 } else if (S_ISDIR(tmp_inode->i_mode)) {
889 cFYI(1, ("Directory inode"));
890 tmp_inode->i_op = &cifs_dir_inode_ops;
891 tmp_inode->i_fop = &cifs_dir_ops;
892 } else if (S_ISLNK(tmp_inode->i_mode)) {
893 cFYI(1, ("Symbolic Link inode"));
894 tmp_inode->i_op = &cifs_symlink_inode_ops;
895 /* tmp_inode->i_fop = *//* do not need to set to anything */
896 } else {
897 cFYI(1, ("Special inode"));
898 init_special_inode(tmp_inode, tmp_inode->i_mode,
899 tmp_inode->i_rdev);
903 int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
905 int rc = 0;
906 int xid;
907 struct cifs_sb_info *cifs_sb;
908 struct cifsTconInfo *pTcon;
909 char *full_path = NULL;
910 struct inode *newinode = NULL;
912 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
914 xid = GetXid();
916 cifs_sb = CIFS_SB(inode->i_sb);
917 pTcon = cifs_sb->tcon;
919 full_path = build_path_from_dentry(direntry);
920 if (full_path == NULL) {
921 FreeXid(xid);
922 return -ENOMEM;
925 if ((pTcon->ses->capabilities & CAP_UNIX) &&
926 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
927 le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
928 u32 oplock = 0;
929 FILE_UNIX_BASIC_INFO * pInfo =
930 kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
931 if (pInfo == NULL) {
932 rc = -ENOMEM;
933 goto mkdir_out;
936 mode &= ~current->fs->umask;
937 rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT,
938 mode, NULL /* netfid */, pInfo, &oplock,
939 full_path, cifs_sb->local_nls,
940 cifs_sb->mnt_cifs_flags &
941 CIFS_MOUNT_MAP_SPECIAL_CHR);
942 if (rc == -EOPNOTSUPP) {
943 kfree(pInfo);
944 goto mkdir_retry_old;
945 } else if (rc) {
946 cFYI(1, ("posix mkdir returned 0x%x", rc));
947 d_drop(direntry);
948 } else {
949 int obj_type;
950 if (pInfo->Type == cpu_to_le32(-1)) {
951 /* no return info, go query for it */
952 kfree(pInfo);
953 goto mkdir_get_info;
955 /*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need
956 to set uid/gid */
957 inc_nlink(inode);
958 if (pTcon->nocase)
959 direntry->d_op = &cifs_ci_dentry_ops;
960 else
961 direntry->d_op = &cifs_dentry_ops;
963 newinode = new_inode(inode->i_sb);
964 if (newinode == NULL) {
965 kfree(pInfo);
966 goto mkdir_get_info;
968 /* Is an i_ino of zero legal? */
969 /* Are there sanity checks we can use to ensure that
970 the server is really filling in that field? */
971 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
972 newinode->i_ino =
973 (unsigned long)pInfo->UniqueId;
974 } /* note ino incremented to unique num in new_inode */
975 if (inode->i_sb->s_flags & MS_NOATIME)
976 newinode->i_flags |= S_NOATIME | S_NOCMTIME;
977 newinode->i_nlink = 2;
979 insert_inode_hash(newinode);
980 d_instantiate(direntry, newinode);
982 /* we already checked in POSIXCreate whether
983 frame was long enough */
984 posix_fill_in_inode(direntry->d_inode,
985 pInfo, &obj_type, 1 /* NewInode */);
986 #ifdef CONFIG_CIFS_DEBUG2
987 cFYI(1, ("instantiated dentry %p %s to inode %p",
988 direntry, direntry->d_name.name, newinode));
990 if (newinode->i_nlink != 2)
991 cFYI(1, ("unexpected number of links %d",
992 newinode->i_nlink));
993 #endif
995 kfree(pInfo);
996 goto mkdir_out;
998 mkdir_retry_old:
999 /* BB add setting the equivalent of mode via CreateX w/ACLs */
1000 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
1001 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1002 if (rc) {
1003 cFYI(1, ("cifs_mkdir returned 0x%x", rc));
1004 d_drop(direntry);
1005 } else {
1006 mkdir_get_info:
1007 inc_nlink(inode);
1008 if (pTcon->unix_ext)
1009 rc = cifs_get_inode_info_unix(&newinode, full_path,
1010 inode->i_sb, xid);
1011 else
1012 rc = cifs_get_inode_info(&newinode, full_path, NULL,
1013 inode->i_sb, xid);
1015 if (pTcon->nocase)
1016 direntry->d_op = &cifs_ci_dentry_ops;
1017 else
1018 direntry->d_op = &cifs_dentry_ops;
1019 d_instantiate(direntry, newinode);
1020 /* setting nlink not necessary except in cases where we
1021 * failed to get it from the server or was set bogus */
1022 if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2))
1023 direntry->d_inode->i_nlink = 2;
1024 if (pTcon->unix_ext) {
1025 mode &= ~current->fs->umask;
1026 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1027 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
1028 mode,
1029 (__u64)current->fsuid,
1030 (__u64)current->fsgid,
1031 0 /* dev_t */,
1032 cifs_sb->local_nls,
1033 cifs_sb->mnt_cifs_flags &
1034 CIFS_MOUNT_MAP_SPECIAL_CHR);
1035 } else {
1036 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
1037 mode, (__u64)-1,
1038 (__u64)-1, 0 /* dev_t */,
1039 cifs_sb->local_nls,
1040 cifs_sb->mnt_cifs_flags &
1041 CIFS_MOUNT_MAP_SPECIAL_CHR);
1043 } else {
1044 /* BB to be implemented via Windows secrty descriptors
1045 eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
1046 -1, -1, local_nls); */
1047 if (direntry->d_inode) {
1048 direntry->d_inode->i_mode = mode;
1049 direntry->d_inode->i_mode |= S_IFDIR;
1050 if (cifs_sb->mnt_cifs_flags &
1051 CIFS_MOUNT_SET_UID) {
1052 direntry->d_inode->i_uid =
1053 current->fsuid;
1054 direntry->d_inode->i_gid =
1055 current->fsgid;
1060 mkdir_out:
1061 kfree(full_path);
1062 FreeXid(xid);
1063 return rc;
1066 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1068 int rc = 0;
1069 int xid;
1070 struct cifs_sb_info *cifs_sb;
1071 struct cifsTconInfo *pTcon;
1072 char *full_path = NULL;
1073 struct cifsInodeInfo *cifsInode;
1075 cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
1077 xid = GetXid();
1079 cifs_sb = CIFS_SB(inode->i_sb);
1080 pTcon = cifs_sb->tcon;
1082 full_path = build_path_from_dentry(direntry);
1083 if (full_path == NULL) {
1084 FreeXid(xid);
1085 return -ENOMEM;
1088 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
1089 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1091 if (!rc) {
1092 drop_nlink(inode);
1093 spin_lock(&direntry->d_inode->i_lock);
1094 i_size_write(direntry->d_inode, 0);
1095 clear_nlink(direntry->d_inode);
1096 spin_unlock(&direntry->d_inode->i_lock);
1099 cifsInode = CIFS_I(direntry->d_inode);
1100 cifsInode->time = 0; /* force revalidate to go get info when
1101 needed */
1102 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
1103 current_fs_time(inode->i_sb);
1105 kfree(full_path);
1106 FreeXid(xid);
1107 return rc;
1110 int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
1111 struct inode *target_inode, struct dentry *target_direntry)
1113 char *fromName;
1114 char *toName;
1115 struct cifs_sb_info *cifs_sb_source;
1116 struct cifs_sb_info *cifs_sb_target;
1117 struct cifsTconInfo *pTcon;
1118 int xid;
1119 int rc = 0;
1121 xid = GetXid();
1123 cifs_sb_target = CIFS_SB(target_inode->i_sb);
1124 cifs_sb_source = CIFS_SB(source_inode->i_sb);
1125 pTcon = cifs_sb_source->tcon;
1127 if (pTcon != cifs_sb_target->tcon) {
1128 FreeXid(xid);
1129 return -EXDEV; /* BB actually could be allowed if same server,
1130 but different share.
1131 Might eventually add support for this */
1134 /* we already have the rename sem so we do not need to grab it again
1135 here to protect the path integrity */
1136 fromName = build_path_from_dentry(source_direntry);
1137 toName = build_path_from_dentry(target_direntry);
1138 if ((fromName == NULL) || (toName == NULL)) {
1139 rc = -ENOMEM;
1140 goto cifs_rename_exit;
1143 rc = CIFSSMBRename(xid, pTcon, fromName, toName,
1144 cifs_sb_source->local_nls,
1145 cifs_sb_source->mnt_cifs_flags &
1146 CIFS_MOUNT_MAP_SPECIAL_CHR);
1147 if (rc == -EEXIST) {
1148 /* check if they are the same file because rename of hardlinked
1149 files is a noop */
1150 FILE_UNIX_BASIC_INFO *info_buf_source;
1151 FILE_UNIX_BASIC_INFO *info_buf_target;
1153 info_buf_source =
1154 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1155 if (info_buf_source != NULL) {
1156 info_buf_target = info_buf_source + 1;
1157 if (pTcon->unix_ext)
1158 rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
1159 info_buf_source,
1160 cifs_sb_source->local_nls,
1161 cifs_sb_source->mnt_cifs_flags &
1162 CIFS_MOUNT_MAP_SPECIAL_CHR);
1163 /* else rc is still EEXIST so will fall through to
1164 unlink the target and retry rename */
1165 if (rc == 0) {
1166 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
1167 info_buf_target,
1168 cifs_sb_target->local_nls,
1169 /* remap based on source sb */
1170 cifs_sb_source->mnt_cifs_flags &
1171 CIFS_MOUNT_MAP_SPECIAL_CHR);
1173 if ((rc == 0) &&
1174 (info_buf_source->UniqueId ==
1175 info_buf_target->UniqueId)) {
1176 /* do not rename since the files are hardlinked which
1177 is a noop */
1178 } else {
1179 /* we either can not tell the files are hardlinked
1180 (as with Windows servers) or files are not
1181 hardlinked so delete the target manually before
1182 renaming to follow POSIX rather than Windows
1183 semantics */
1184 cifs_unlink(target_inode, target_direntry);
1185 rc = CIFSSMBRename(xid, pTcon, fromName,
1186 toName,
1187 cifs_sb_source->local_nls,
1188 cifs_sb_source->mnt_cifs_flags
1189 & CIFS_MOUNT_MAP_SPECIAL_CHR);
1191 kfree(info_buf_source);
1192 } /* if we can not get memory just leave rc as EEXIST */
1195 if (rc) {
1196 cFYI(1, ("rename rc %d", rc));
1199 if ((rc == -EIO) || (rc == -EEXIST)) {
1200 int oplock = FALSE;
1201 __u16 netfid;
1203 /* BB FIXME Is Generic Read correct for rename? */
1204 /* if renaming directory - we should not say CREATE_NOT_DIR,
1205 need to test renaming open directory, also GENERIC_READ
1206 might not right be right access to request */
1207 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
1208 CREATE_NOT_DIR, &netfid, &oplock, NULL,
1209 cifs_sb_source->local_nls,
1210 cifs_sb_source->mnt_cifs_flags &
1211 CIFS_MOUNT_MAP_SPECIAL_CHR);
1212 if (rc == 0) {
1213 rc = CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
1214 cifs_sb_source->local_nls,
1215 cifs_sb_source->mnt_cifs_flags &
1216 CIFS_MOUNT_MAP_SPECIAL_CHR);
1217 CIFSSMBClose(xid, pTcon, netfid);
1221 cifs_rename_exit:
1222 kfree(fromName);
1223 kfree(toName);
1224 FreeXid(xid);
1225 return rc;
1228 int cifs_revalidate(struct dentry *direntry)
1230 int xid;
1231 int rc = 0;
1232 char *full_path;
1233 struct cifs_sb_info *cifs_sb;
1234 struct cifsInodeInfo *cifsInode;
1235 loff_t local_size;
1236 struct timespec local_mtime;
1237 int invalidate_inode = FALSE;
1239 if (direntry->d_inode == NULL)
1240 return -ENOENT;
1242 cifsInode = CIFS_I(direntry->d_inode);
1244 if (cifsInode == NULL)
1245 return -ENOENT;
1247 /* no sense revalidating inode info on file that no one can write */
1248 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
1249 return rc;
1251 xid = GetXid();
1253 cifs_sb = CIFS_SB(direntry->d_sb);
1255 /* can not safely grab the rename sem here if rename calls revalidate
1256 since that would deadlock */
1257 full_path = build_path_from_dentry(direntry);
1258 if (full_path == NULL) {
1259 FreeXid(xid);
1260 return -ENOMEM;
1262 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
1263 "jiffies %ld", full_path, direntry->d_inode,
1264 direntry->d_inode->i_count.counter, direntry,
1265 direntry->d_time, jiffies));
1267 if (cifsInode->time == 0) {
1268 /* was set to zero previously to force revalidate */
1269 } else if (time_before(jiffies, cifsInode->time + HZ) &&
1270 lookupCacheEnabled) {
1271 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
1272 (direntry->d_inode->i_nlink == 1)) {
1273 kfree(full_path);
1274 FreeXid(xid);
1275 return rc;
1276 } else {
1277 cFYI(1, ("Have to revalidate file due to hardlinks"));
1281 /* save mtime and size */
1282 local_mtime = direntry->d_inode->i_mtime;
1283 local_size = direntry->d_inode->i_size;
1285 if (cifs_sb->tcon->unix_ext) {
1286 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
1287 direntry->d_sb, xid);
1288 if (rc) {
1289 cFYI(1, ("error on getting revalidate info %d", rc));
1290 /* if (rc != -ENOENT)
1291 rc = 0; */ /* BB should we cache info on
1292 certain errors? */
1294 } else {
1295 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
1296 direntry->d_sb, xid);
1297 if (rc) {
1298 cFYI(1, ("error on getting revalidate info %d", rc));
1299 /* if (rc != -ENOENT)
1300 rc = 0; */ /* BB should we cache info on
1301 certain errors? */
1304 /* should we remap certain errors, access denied?, to zero */
1306 /* if not oplocked, we invalidate inode pages if mtime or file size
1307 had changed on server */
1309 if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) &&
1310 (local_size == direntry->d_inode->i_size)) {
1311 cFYI(1, ("cifs_revalidate - inode unchanged"));
1312 } else {
1313 /* file may have changed on server */
1314 if (cifsInode->clientCanCacheRead) {
1315 /* no need to invalidate inode pages since we were the
1316 only ones who could have modified the file and the
1317 server copy is staler than ours */
1318 } else {
1319 invalidate_inode = TRUE;
1323 /* can not grab this sem since kernel filesys locking documentation
1324 indicates i_mutex may be taken by the kernel on lookup and rename
1325 which could deadlock if we grab the i_mutex here as well */
1326 /* mutex_lock(&direntry->d_inode->i_mutex);*/
1327 /* need to write out dirty pages here */
1328 if (direntry->d_inode->i_mapping) {
1329 /* do we need to lock inode until after invalidate completes
1330 below? */
1331 filemap_fdatawrite(direntry->d_inode->i_mapping);
1333 if (invalidate_inode) {
1334 /* shrink_dcache not necessary now that cifs dentry ops
1335 are exported for negative dentries */
1336 /* if (S_ISDIR(direntry->d_inode->i_mode))
1337 shrink_dcache_parent(direntry); */
1338 if (S_ISREG(direntry->d_inode->i_mode)) {
1339 if (direntry->d_inode->i_mapping)
1340 filemap_fdatawait(direntry->d_inode->i_mapping);
1341 /* may eventually have to do this for open files too */
1342 if (list_empty(&(cifsInode->openFileList))) {
1343 /* changed on server - flush read ahead pages */
1344 cFYI(1, ("Invalidating read ahead data on "
1345 "closed file"));
1346 invalidate_remote_inode(direntry->d_inode);
1350 /* mutex_unlock(&direntry->d_inode->i_mutex); */
1352 kfree(full_path);
1353 FreeXid(xid);
1354 return rc;
1357 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1358 struct kstat *stat)
1360 int err = cifs_revalidate(dentry);
1361 if (!err) {
1362 generic_fillattr(dentry->d_inode, stat);
1363 stat->blksize = CIFS_MAX_MSGSIZE;
1365 return err;
1368 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1370 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1371 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1372 struct page *page;
1373 int rc = 0;
1375 page = grab_cache_page(mapping, index);
1376 if (!page)
1377 return -ENOMEM;
1379 zero_user_page(page, offset, PAGE_CACHE_SIZE - offset, KM_USER0);
1380 unlock_page(page);
1381 page_cache_release(page);
1382 return rc;
1385 static int cifs_vmtruncate(struct inode *inode, loff_t offset)
1387 struct address_space *mapping = inode->i_mapping;
1388 unsigned long limit;
1390 spin_lock(&inode->i_lock);
1391 if (inode->i_size < offset)
1392 goto do_expand;
1394 * truncation of in-use swapfiles is disallowed - it would cause
1395 * subsequent swapout to scribble on the now-freed blocks.
1397 if (IS_SWAPFILE(inode)) {
1398 spin_unlock(&inode->i_lock);
1399 goto out_busy;
1401 i_size_write(inode, offset);
1402 spin_unlock(&inode->i_lock);
1404 * unmap_mapping_range is called twice, first simply for efficiency
1405 * so that truncate_inode_pages does fewer single-page unmaps. However
1406 * after this first call, and before truncate_inode_pages finishes,
1407 * it is possible for private pages to be COWed, which remain after
1408 * truncate_inode_pages finishes, hence the second unmap_mapping_range
1409 * call must be made for correctness.
1411 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1412 truncate_inode_pages(mapping, offset);
1413 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1414 goto out_truncate;
1416 do_expand:
1417 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1418 if (limit != RLIM_INFINITY && offset > limit) {
1419 spin_unlock(&inode->i_lock);
1420 goto out_sig;
1422 if (offset > inode->i_sb->s_maxbytes) {
1423 spin_unlock(&inode->i_lock);
1424 goto out_big;
1426 i_size_write(inode, offset);
1427 spin_unlock(&inode->i_lock);
1428 out_truncate:
1429 if (inode->i_op && inode->i_op->truncate)
1430 inode->i_op->truncate(inode);
1431 return 0;
1432 out_sig:
1433 send_sig(SIGXFSZ, current, 0);
1434 out_big:
1435 return -EFBIG;
1436 out_busy:
1437 return -ETXTBSY;
1440 int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
1442 int xid;
1443 struct cifs_sb_info *cifs_sb;
1444 struct cifsTconInfo *pTcon;
1445 char *full_path = NULL;
1446 int rc = -EACCES;
1447 struct cifsFileInfo *open_file = NULL;
1448 FILE_BASIC_INFO time_buf;
1449 int set_time = FALSE;
1450 int set_dosattr = FALSE;
1451 __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
1452 __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
1453 __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
1454 struct cifsInodeInfo *cifsInode;
1456 xid = GetXid();
1458 cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
1459 direntry->d_name.name, attrs->ia_valid));
1461 cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
1462 pTcon = cifs_sb->tcon;
1464 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
1465 /* check if we have permission to change attrs */
1466 rc = inode_change_ok(direntry->d_inode, attrs);
1467 if (rc < 0) {
1468 FreeXid(xid);
1469 return rc;
1470 } else
1471 rc = 0;
1474 full_path = build_path_from_dentry(direntry);
1475 if (full_path == NULL) {
1476 FreeXid(xid);
1477 return -ENOMEM;
1479 cifsInode = CIFS_I(direntry->d_inode);
1481 /* BB check if we need to refresh inode from server now ? BB */
1483 /* need to flush data before changing file size on server */
1484 filemap_write_and_wait(direntry->d_inode->i_mapping);
1486 if (attrs->ia_valid & ATTR_SIZE) {
1487 /* To avoid spurious oplock breaks from server, in the case of
1488 inodes that we already have open, avoid doing path based
1489 setting of file size if we can do it by handle.
1490 This keeps our caching token (oplock) and avoids timeouts
1491 when the local oplock break takes longer to flush
1492 writebehind data than the SMB timeout for the SetPathInfo
1493 request would allow */
1495 open_file = find_writable_file(cifsInode);
1496 if (open_file) {
1497 __u16 nfid = open_file->netfid;
1498 __u32 npid = open_file->pid;
1499 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
1500 nfid, npid, FALSE);
1501 atomic_dec(&open_file->wrtPending);
1502 cFYI(1, ("SetFSize for attrs rc = %d", rc));
1503 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1504 unsigned int bytes_written;
1505 rc = CIFSSMBWrite(xid, pTcon,
1506 nfid, 0, attrs->ia_size,
1507 &bytes_written, NULL, NULL,
1508 1 /* 45 seconds */);
1509 cFYI(1, ("Wrt seteof rc %d", rc));
1511 } else
1512 rc = -EINVAL;
1514 if (rc != 0) {
1515 /* Set file size by pathname rather than by handle
1516 either because no valid, writeable file handle for
1517 it was found or because there was an error setting
1518 it by handle */
1519 rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1520 attrs->ia_size, FALSE,
1521 cifs_sb->local_nls,
1522 cifs_sb->mnt_cifs_flags &
1523 CIFS_MOUNT_MAP_SPECIAL_CHR);
1524 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1525 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1526 __u16 netfid;
1527 int oplock = FALSE;
1529 rc = SMBLegacyOpen(xid, pTcon, full_path,
1530 FILE_OPEN,
1531 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1532 CREATE_NOT_DIR, &netfid, &oplock,
1533 NULL, cifs_sb->local_nls,
1534 cifs_sb->mnt_cifs_flags &
1535 CIFS_MOUNT_MAP_SPECIAL_CHR);
1536 if (rc == 0) {
1537 unsigned int bytes_written;
1538 rc = CIFSSMBWrite(xid, pTcon,
1539 netfid, 0,
1540 attrs->ia_size,
1541 &bytes_written, NULL,
1542 NULL, 1 /* 45 sec */);
1543 cFYI(1, ("wrt seteof rc %d", rc));
1544 CIFSSMBClose(xid, pTcon, netfid);
1550 /* Server is ok setting allocation size implicitly - no need
1551 to call:
1552 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
1553 cifs_sb->local_nls);
1556 if (rc == 0) {
1557 rc = cifs_vmtruncate(direntry->d_inode, attrs->ia_size);
1558 cifs_truncate_page(direntry->d_inode->i_mapping,
1559 direntry->d_inode->i_size);
1560 } else
1561 goto cifs_setattr_exit;
1563 if (attrs->ia_valid & ATTR_UID) {
1564 cFYI(1, ("UID changed to %d", attrs->ia_uid));
1565 uid = attrs->ia_uid;
1567 if (attrs->ia_valid & ATTR_GID) {
1568 cFYI(1, ("GID changed to %d", attrs->ia_gid));
1569 gid = attrs->ia_gid;
1572 time_buf.Attributes = 0;
1574 /* skip mode change if it's just for clearing setuid/setgid */
1575 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1576 attrs->ia_valid &= ~ATTR_MODE;
1578 if (attrs->ia_valid & ATTR_MODE) {
1579 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
1580 mode = attrs->ia_mode;
1583 if ((pTcon->unix_ext)
1584 && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
1585 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
1586 0 /* dev_t */, cifs_sb->local_nls,
1587 cifs_sb->mnt_cifs_flags &
1588 CIFS_MOUNT_MAP_SPECIAL_CHR);
1589 else if (attrs->ia_valid & ATTR_MODE) {
1590 rc = 0;
1591 if ((mode & S_IWUGO) == 0) /* not writeable */ {
1592 if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
1593 set_dosattr = TRUE;
1594 time_buf.Attributes =
1595 cpu_to_le32(cifsInode->cifsAttrs |
1596 ATTR_READONLY);
1598 } else if (cifsInode->cifsAttrs & ATTR_READONLY) {
1599 /* If file is readonly on server, we would
1600 not be able to write to it - so if any write
1601 bit is enabled for user or group or other we
1602 need to at least try to remove r/o dos attr */
1603 set_dosattr = TRUE;
1604 time_buf.Attributes = cpu_to_le32(cifsInode->cifsAttrs &
1605 (~ATTR_READONLY));
1606 /* Windows ignores set to zero */
1607 if (time_buf.Attributes == 0)
1608 time_buf.Attributes |= cpu_to_le32(ATTR_NORMAL);
1610 /* BB to be implemented -
1611 via Windows security descriptors or streams */
1612 /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid,
1613 cifs_sb->local_nls); */
1616 if (attrs->ia_valid & ATTR_ATIME) {
1617 set_time = TRUE;
1618 time_buf.LastAccessTime =
1619 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1620 } else
1621 time_buf.LastAccessTime = 0;
1623 if (attrs->ia_valid & ATTR_MTIME) {
1624 set_time = TRUE;
1625 time_buf.LastWriteTime =
1626 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1627 } else
1628 time_buf.LastWriteTime = 0;
1629 /* Do not set ctime explicitly unless other time
1630 stamps are changed explicitly (i.e. by utime()
1631 since we would then have a mix of client and
1632 server times */
1634 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1635 set_time = TRUE;
1636 /* Although Samba throws this field away
1637 it may be useful to Windows - but we do
1638 not want to set ctime unless some other
1639 timestamp is changing */
1640 cFYI(1, ("CIFS - CTIME changed"));
1641 time_buf.ChangeTime =
1642 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1643 } else
1644 time_buf.ChangeTime = 0;
1646 if (set_time || set_dosattr) {
1647 time_buf.CreationTime = 0; /* do not change */
1648 /* In the future we should experiment - try setting timestamps
1649 via Handle (SetFileInfo) instead of by path */
1650 if (!(pTcon->ses->flags & CIFS_SES_NT4))
1651 rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
1652 cifs_sb->local_nls,
1653 cifs_sb->mnt_cifs_flags &
1654 CIFS_MOUNT_MAP_SPECIAL_CHR);
1655 else
1656 rc = -EOPNOTSUPP;
1658 if (rc == -EOPNOTSUPP) {
1659 int oplock = FALSE;
1660 __u16 netfid;
1662 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
1663 "times not supported by this server"));
1664 /* BB we could scan to see if we already have it open
1665 and pass in pid of opener to function */
1666 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1667 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1668 CREATE_NOT_DIR, &netfid, &oplock,
1669 NULL, cifs_sb->local_nls,
1670 cifs_sb->mnt_cifs_flags &
1671 CIFS_MOUNT_MAP_SPECIAL_CHR);
1672 if (rc == 0) {
1673 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1674 netfid);
1675 CIFSSMBClose(xid, pTcon, netfid);
1676 } else {
1677 /* BB For even older servers we could convert time_buf
1678 into old DOS style which uses two second
1679 granularity */
1681 /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
1682 &time_buf, cifs_sb->local_nls); */
1685 /* Even if error on time set, no sense failing the call if
1686 the server would set the time to a reasonable value anyway,
1687 and this check ensures that we are not being called from
1688 sys_utimes in which case we ought to fail the call back to
1689 the user when the server rejects the call */
1690 if ((rc) && (attrs->ia_valid &
1691 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
1692 rc = 0;
1695 /* do not need local check to inode_check_ok since the server does
1696 that */
1697 if (!rc)
1698 rc = inode_setattr(direntry->d_inode, attrs);
1699 cifs_setattr_exit:
1700 kfree(full_path);
1701 FreeXid(xid);
1702 return rc;
1705 #if 0
1706 void cifs_delete_inode(struct inode *inode)
1708 cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
1709 /* may have to add back in if and when safe distributed caching of
1710 directories added e.g. via FindNotify */
1712 #endif