Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[linux-2.6.git] / fs / cifs / inode.c
blob36f9ebb93ceba676c363cdf7b973f3936a9dde8f
1 /*
2 * fs/cifs/inode.c
4 * Copyright (C) International Business Machines Corp., 2002,2010
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/slab.h>
24 #include <linux/pagemap.h>
25 #include <asm/div64.h>
26 #include "cifsfs.h"
27 #include "cifspdu.h"
28 #include "cifsglob.h"
29 #include "cifsproto.h"
30 #include "cifs_debug.h"
31 #include "cifs_fs_sb.h"
32 #include "fscache.h"
35 static void cifs_set_ops(struct inode *inode)
37 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
39 switch (inode->i_mode & S_IFMT) {
40 case S_IFREG:
41 inode->i_op = &cifs_file_inode_ops;
42 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
43 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
44 inode->i_fop = &cifs_file_direct_nobrl_ops;
45 else
46 inode->i_fop = &cifs_file_direct_ops;
47 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
48 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
49 inode->i_fop = &cifs_file_strict_nobrl_ops;
50 else
51 inode->i_fop = &cifs_file_strict_ops;
52 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
53 inode->i_fop = &cifs_file_nobrl_ops;
54 else { /* not direct, send byte range locks */
55 inode->i_fop = &cifs_file_ops;
58 /* check if server can support readpages */
59 if (cifs_sb_master_tcon(cifs_sb)->ses->server->maxBuf <
60 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
61 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
62 else
63 inode->i_data.a_ops = &cifs_addr_ops;
64 break;
65 case S_IFDIR:
66 #ifdef CONFIG_CIFS_DFS_UPCALL
67 if (IS_AUTOMOUNT(inode)) {
68 inode->i_op = &cifs_dfs_referral_inode_operations;
69 } else {
70 #else /* NO DFS support, treat as a directory */
72 #endif
73 inode->i_op = &cifs_dir_inode_ops;
74 inode->i_fop = &cifs_dir_ops;
76 break;
77 case S_IFLNK:
78 inode->i_op = &cifs_symlink_inode_ops;
79 break;
80 default:
81 init_special_inode(inode, inode->i_mode, inode->i_rdev);
82 break;
86 /* check inode attributes against fattr. If they don't match, tag the
87 * inode for cache invalidation
89 static void
90 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
92 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
94 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
95 __func__, cifs_i->uniqueid);
97 if (inode->i_state & I_NEW) {
98 cifs_dbg(FYI, "%s: inode %llu is new\n",
99 __func__, cifs_i->uniqueid);
100 return;
103 /* don't bother with revalidation if we have an oplock */
104 if (CIFS_CACHE_READ(cifs_i)) {
105 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
106 __func__, cifs_i->uniqueid);
107 return;
110 /* revalidate if mtime or size have changed */
111 if (timespec_equal(&inode->i_mtime, &fattr->cf_mtime) &&
112 cifs_i->server_eof == fattr->cf_eof) {
113 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
114 __func__, cifs_i->uniqueid);
115 return;
118 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
119 __func__, cifs_i->uniqueid);
120 cifs_i->invalid_mapping = true;
124 * copy nlink to the inode, unless it wasn't provided. Provide
125 * sane values if we don't have an existing one and none was provided
127 static void
128 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
131 * if we're in a situation where we can't trust what we
132 * got from the server (readdir, some non-unix cases)
133 * fake reasonable values
135 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
136 /* only provide fake values on a new inode */
137 if (inode->i_state & I_NEW) {
138 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
139 set_nlink(inode, 2);
140 else
141 set_nlink(inode, 1);
143 return;
146 /* we trust the server, so update it */
147 set_nlink(inode, fattr->cf_nlink);
150 /* populate an inode with info from a cifs_fattr struct */
151 void
152 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
154 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
155 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
157 cifs_revalidate_cache(inode, fattr);
159 spin_lock(&inode->i_lock);
160 inode->i_atime = fattr->cf_atime;
161 inode->i_mtime = fattr->cf_mtime;
162 inode->i_ctime = fattr->cf_ctime;
163 inode->i_rdev = fattr->cf_rdev;
164 cifs_nlink_fattr_to_inode(inode, fattr);
165 inode->i_uid = fattr->cf_uid;
166 inode->i_gid = fattr->cf_gid;
168 /* if dynperm is set, don't clobber existing mode */
169 if (inode->i_state & I_NEW ||
170 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
171 inode->i_mode = fattr->cf_mode;
173 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
175 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
176 cifs_i->time = 0;
177 else
178 cifs_i->time = jiffies;
180 cifs_i->delete_pending = fattr->cf_flags & CIFS_FATTR_DELETE_PENDING;
182 cifs_i->server_eof = fattr->cf_eof;
184 * Can't safely change the file size here if the client is writing to
185 * it due to potential races.
187 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
188 i_size_write(inode, fattr->cf_eof);
191 * i_blocks is not related to (i_size / i_blksize),
192 * but instead 512 byte (2**9) size is required for
193 * calculating num blocks.
195 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
197 spin_unlock(&inode->i_lock);
199 if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
200 inode->i_flags |= S_AUTOMOUNT;
201 if (inode->i_state & I_NEW)
202 cifs_set_ops(inode);
205 void
206 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
208 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
210 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
211 return;
213 fattr->cf_uniqueid = iunique(sb, ROOT_I);
216 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
217 void
218 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
219 struct cifs_sb_info *cifs_sb)
221 memset(fattr, 0, sizeof(*fattr));
222 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
223 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
224 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
226 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
227 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
228 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
229 fattr->cf_mode = le64_to_cpu(info->Permissions);
232 * Since we set the inode type below we need to mask off
233 * to avoid strange results if bits set above.
235 fattr->cf_mode &= ~S_IFMT;
236 switch (le32_to_cpu(info->Type)) {
237 case UNIX_FILE:
238 fattr->cf_mode |= S_IFREG;
239 fattr->cf_dtype = DT_REG;
240 break;
241 case UNIX_SYMLINK:
242 fattr->cf_mode |= S_IFLNK;
243 fattr->cf_dtype = DT_LNK;
244 break;
245 case UNIX_DIR:
246 fattr->cf_mode |= S_IFDIR;
247 fattr->cf_dtype = DT_DIR;
248 break;
249 case UNIX_CHARDEV:
250 fattr->cf_mode |= S_IFCHR;
251 fattr->cf_dtype = DT_CHR;
252 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
253 le64_to_cpu(info->DevMinor) & MINORMASK);
254 break;
255 case UNIX_BLOCKDEV:
256 fattr->cf_mode |= S_IFBLK;
257 fattr->cf_dtype = DT_BLK;
258 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
259 le64_to_cpu(info->DevMinor) & MINORMASK);
260 break;
261 case UNIX_FIFO:
262 fattr->cf_mode |= S_IFIFO;
263 fattr->cf_dtype = DT_FIFO;
264 break;
265 case UNIX_SOCKET:
266 fattr->cf_mode |= S_IFSOCK;
267 fattr->cf_dtype = DT_SOCK;
268 break;
269 default:
270 /* safest to call it a file if we do not know */
271 fattr->cf_mode |= S_IFREG;
272 fattr->cf_dtype = DT_REG;
273 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
274 break;
277 fattr->cf_uid = cifs_sb->mnt_uid;
278 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
279 u64 id = le64_to_cpu(info->Uid);
280 if (id < ((uid_t)-1)) {
281 kuid_t uid = make_kuid(&init_user_ns, id);
282 if (uid_valid(uid))
283 fattr->cf_uid = uid;
287 fattr->cf_gid = cifs_sb->mnt_gid;
288 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
289 u64 id = le64_to_cpu(info->Gid);
290 if (id < ((gid_t)-1)) {
291 kgid_t gid = make_kgid(&init_user_ns, id);
292 if (gid_valid(gid))
293 fattr->cf_gid = gid;
297 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
301 * Fill a cifs_fattr struct with fake inode info.
303 * Needed to setup cifs_fattr data for the directory which is the
304 * junction to the new submount (ie to setup the fake directory
305 * which represents a DFS referral).
307 static void
308 cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
310 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
312 cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
314 memset(fattr, 0, sizeof(*fattr));
315 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
316 fattr->cf_uid = cifs_sb->mnt_uid;
317 fattr->cf_gid = cifs_sb->mnt_gid;
318 fattr->cf_atime = CURRENT_TIME;
319 fattr->cf_ctime = CURRENT_TIME;
320 fattr->cf_mtime = CURRENT_TIME;
321 fattr->cf_nlink = 2;
322 fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
325 static int
326 cifs_get_file_info_unix(struct file *filp)
328 int rc;
329 unsigned int xid;
330 FILE_UNIX_BASIC_INFO find_data;
331 struct cifs_fattr fattr;
332 struct inode *inode = file_inode(filp);
333 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
334 struct cifsFileInfo *cfile = filp->private_data;
335 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
337 xid = get_xid();
338 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
339 if (!rc) {
340 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
341 } else if (rc == -EREMOTE) {
342 cifs_create_dfs_fattr(&fattr, inode->i_sb);
343 rc = 0;
346 cifs_fattr_to_inode(inode, &fattr);
347 free_xid(xid);
348 return rc;
351 int cifs_get_inode_info_unix(struct inode **pinode,
352 const unsigned char *full_path,
353 struct super_block *sb, unsigned int xid)
355 int rc;
356 FILE_UNIX_BASIC_INFO find_data;
357 struct cifs_fattr fattr;
358 struct cifs_tcon *tcon;
359 struct tcon_link *tlink;
360 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
362 cifs_dbg(FYI, "Getting info on %s\n", full_path);
364 tlink = cifs_sb_tlink(cifs_sb);
365 if (IS_ERR(tlink))
366 return PTR_ERR(tlink);
367 tcon = tlink_tcon(tlink);
369 /* could have done a find first instead but this returns more info */
370 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
371 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
372 CIFS_MOUNT_MAP_SPECIAL_CHR);
373 cifs_put_tlink(tlink);
375 if (!rc) {
376 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
377 } else if (rc == -EREMOTE) {
378 cifs_create_dfs_fattr(&fattr, sb);
379 rc = 0;
380 } else {
381 return rc;
384 /* check for Minshall+French symlinks */
385 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
386 int tmprc = CIFSCheckMFSymlink(&fattr, full_path, cifs_sb, xid);
387 if (tmprc)
388 cifs_dbg(FYI, "CIFSCheckMFSymlink: %d\n", tmprc);
391 if (*pinode == NULL) {
392 /* get new inode */
393 cifs_fill_uniqueid(sb, &fattr);
394 *pinode = cifs_iget(sb, &fattr);
395 if (!*pinode)
396 rc = -ENOMEM;
397 } else {
398 /* we already have inode, update it */
399 cifs_fattr_to_inode(*pinode, &fattr);
402 return rc;
405 static int
406 cifs_sfu_type(struct cifs_fattr *fattr, const unsigned char *path,
407 struct cifs_sb_info *cifs_sb, unsigned int xid)
409 int rc;
410 int oplock = 0;
411 __u16 netfid;
412 struct tcon_link *tlink;
413 struct cifs_tcon *tcon;
414 struct cifs_io_parms io_parms;
415 char buf[24];
416 unsigned int bytes_read;
417 char *pbuf;
419 pbuf = buf;
421 fattr->cf_mode &= ~S_IFMT;
423 if (fattr->cf_eof == 0) {
424 fattr->cf_mode |= S_IFIFO;
425 fattr->cf_dtype = DT_FIFO;
426 return 0;
427 } else if (fattr->cf_eof < 8) {
428 fattr->cf_mode |= S_IFREG;
429 fattr->cf_dtype = DT_REG;
430 return -EINVAL; /* EOPNOTSUPP? */
433 tlink = cifs_sb_tlink(cifs_sb);
434 if (IS_ERR(tlink))
435 return PTR_ERR(tlink);
436 tcon = tlink_tcon(tlink);
438 rc = CIFSSMBOpen(xid, tcon, path, FILE_OPEN, GENERIC_READ,
439 CREATE_NOT_DIR, &netfid, &oplock, NULL,
440 cifs_sb->local_nls,
441 cifs_sb->mnt_cifs_flags &
442 CIFS_MOUNT_MAP_SPECIAL_CHR);
443 if (rc == 0) {
444 int buf_type = CIFS_NO_BUFFER;
445 /* Read header */
446 io_parms.netfid = netfid;
447 io_parms.pid = current->tgid;
448 io_parms.tcon = tcon;
449 io_parms.offset = 0;
450 io_parms.length = 24;
451 rc = CIFSSMBRead(xid, &io_parms, &bytes_read, &pbuf,
452 &buf_type);
453 if ((rc == 0) && (bytes_read >= 8)) {
454 if (memcmp("IntxBLK", pbuf, 8) == 0) {
455 cifs_dbg(FYI, "Block device\n");
456 fattr->cf_mode |= S_IFBLK;
457 fattr->cf_dtype = DT_BLK;
458 if (bytes_read == 24) {
459 /* we have enough to decode dev num */
460 __u64 mjr; /* major */
461 __u64 mnr; /* minor */
462 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
463 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
464 fattr->cf_rdev = MKDEV(mjr, mnr);
466 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
467 cifs_dbg(FYI, "Char device\n");
468 fattr->cf_mode |= S_IFCHR;
469 fattr->cf_dtype = DT_CHR;
470 if (bytes_read == 24) {
471 /* we have enough to decode dev num */
472 __u64 mjr; /* major */
473 __u64 mnr; /* minor */
474 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
475 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
476 fattr->cf_rdev = MKDEV(mjr, mnr);
478 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
479 cifs_dbg(FYI, "Symlink\n");
480 fattr->cf_mode |= S_IFLNK;
481 fattr->cf_dtype = DT_LNK;
482 } else {
483 fattr->cf_mode |= S_IFREG; /* file? */
484 fattr->cf_dtype = DT_REG;
485 rc = -EOPNOTSUPP;
487 } else {
488 fattr->cf_mode |= S_IFREG; /* then it is a file */
489 fattr->cf_dtype = DT_REG;
490 rc = -EOPNOTSUPP; /* or some unknown SFU type */
492 CIFSSMBClose(xid, tcon, netfid);
494 cifs_put_tlink(tlink);
495 return rc;
498 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
501 * Fetch mode bits as provided by SFU.
503 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
505 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
506 struct cifs_sb_info *cifs_sb, unsigned int xid)
508 #ifdef CONFIG_CIFS_XATTR
509 ssize_t rc;
510 char ea_value[4];
511 __u32 mode;
512 struct tcon_link *tlink;
513 struct cifs_tcon *tcon;
515 tlink = cifs_sb_tlink(cifs_sb);
516 if (IS_ERR(tlink))
517 return PTR_ERR(tlink);
518 tcon = tlink_tcon(tlink);
520 rc = CIFSSMBQAllEAs(xid, tcon, path, "SETFILEBITS",
521 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
522 cifs_sb->mnt_cifs_flags &
523 CIFS_MOUNT_MAP_SPECIAL_CHR);
524 cifs_put_tlink(tlink);
525 if (rc < 0)
526 return (int)rc;
527 else if (rc > 3) {
528 mode = le32_to_cpu(*((__le32 *)ea_value));
529 fattr->cf_mode &= ~SFBITS_MASK;
530 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
531 mode, fattr->cf_mode);
532 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
533 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
536 return 0;
537 #else
538 return -EOPNOTSUPP;
539 #endif
542 /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
543 static void
544 cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
545 struct cifs_sb_info *cifs_sb, bool adjust_tz,
546 bool symlink)
548 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
550 memset(fattr, 0, sizeof(*fattr));
551 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
552 if (info->DeletePending)
553 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
555 if (info->LastAccessTime)
556 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
557 else
558 fattr->cf_atime = CURRENT_TIME;
560 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
561 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
563 if (adjust_tz) {
564 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
565 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
568 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
569 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
570 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
572 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
574 if (symlink) {
575 fattr->cf_mode = S_IFLNK;
576 fattr->cf_dtype = DT_LNK;
577 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
578 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
579 fattr->cf_dtype = DT_DIR;
581 * Server can return wrong NumberOfLinks value for directories
582 * when Unix extensions are disabled - fake it.
584 if (!tcon->unix_ext)
585 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
586 } else {
587 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
588 fattr->cf_dtype = DT_REG;
590 /* clear write bits if ATTR_READONLY is set */
591 if (fattr->cf_cifsattrs & ATTR_READONLY)
592 fattr->cf_mode &= ~(S_IWUGO);
595 * Don't accept zero nlink from non-unix servers unless
596 * delete is pending. Instead mark it as unknown.
598 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
599 !info->DeletePending) {
600 cifs_dbg(1, "bogus file nlink value %u\n",
601 fattr->cf_nlink);
602 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
606 fattr->cf_uid = cifs_sb->mnt_uid;
607 fattr->cf_gid = cifs_sb->mnt_gid;
610 static int
611 cifs_get_file_info(struct file *filp)
613 int rc;
614 unsigned int xid;
615 FILE_ALL_INFO find_data;
616 struct cifs_fattr fattr;
617 struct inode *inode = file_inode(filp);
618 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
619 struct cifsFileInfo *cfile = filp->private_data;
620 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
621 struct TCP_Server_Info *server = tcon->ses->server;
623 if (!server->ops->query_file_info)
624 return -ENOSYS;
626 xid = get_xid();
627 rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
628 switch (rc) {
629 case 0:
630 cifs_all_info_to_fattr(&fattr, &find_data, cifs_sb, false,
631 false);
632 break;
633 case -EREMOTE:
634 cifs_create_dfs_fattr(&fattr, inode->i_sb);
635 rc = 0;
636 break;
637 case -EOPNOTSUPP:
638 case -EINVAL:
640 * FIXME: legacy server -- fall back to path-based call?
641 * for now, just skip revalidating and mark inode for
642 * immediate reval.
644 rc = 0;
645 CIFS_I(inode)->time = 0;
646 default:
647 goto cgfi_exit;
651 * don't bother with SFU junk here -- just mark inode as needing
652 * revalidation.
654 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
655 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
656 cifs_fattr_to_inode(inode, &fattr);
657 cgfi_exit:
658 free_xid(xid);
659 return rc;
663 cifs_get_inode_info(struct inode **inode, const char *full_path,
664 FILE_ALL_INFO *data, struct super_block *sb, int xid,
665 const __u16 *fid)
667 bool validinum = false;
668 __u16 srchflgs;
669 int rc = 0, tmprc = ENOSYS;
670 struct cifs_tcon *tcon;
671 struct TCP_Server_Info *server;
672 struct tcon_link *tlink;
673 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
674 char *buf = NULL;
675 bool adjust_tz = false;
676 struct cifs_fattr fattr;
677 struct cifs_search_info *srchinf = NULL;
678 bool symlink = false;
680 tlink = cifs_sb_tlink(cifs_sb);
681 if (IS_ERR(tlink))
682 return PTR_ERR(tlink);
683 tcon = tlink_tcon(tlink);
684 server = tcon->ses->server;
686 cifs_dbg(FYI, "Getting info on %s\n", full_path);
688 if ((data == NULL) && (*inode != NULL)) {
689 if (CIFS_CACHE_READ(CIFS_I(*inode))) {
690 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
691 goto cgii_exit;
695 /* if inode info is not passed, get it from server */
696 if (data == NULL) {
697 if (!server->ops->query_path_info) {
698 rc = -ENOSYS;
699 goto cgii_exit;
701 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
702 if (buf == NULL) {
703 rc = -ENOMEM;
704 goto cgii_exit;
706 data = (FILE_ALL_INFO *)buf;
707 rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path,
708 data, &adjust_tz, &symlink);
711 if (!rc) {
712 cifs_all_info_to_fattr(&fattr, data, cifs_sb, adjust_tz,
713 symlink);
714 } else if (rc == -EREMOTE) {
715 cifs_create_dfs_fattr(&fattr, sb);
716 rc = 0;
717 } else if (rc == -EACCES && backup_cred(cifs_sb)) {
718 srchinf = kzalloc(sizeof(struct cifs_search_info),
719 GFP_KERNEL);
720 if (srchinf == NULL) {
721 rc = -ENOMEM;
722 goto cgii_exit;
725 srchinf->endOfSearch = false;
726 srchinf->info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
728 srchflgs = CIFS_SEARCH_CLOSE_ALWAYS |
729 CIFS_SEARCH_CLOSE_AT_END |
730 CIFS_SEARCH_BACKUP_SEARCH;
732 rc = CIFSFindFirst(xid, tcon, full_path,
733 cifs_sb, NULL, srchflgs, srchinf, false);
734 if (!rc) {
735 data =
736 (FILE_ALL_INFO *)srchinf->srch_entries_start;
738 cifs_dir_info_to_fattr(&fattr,
739 (FILE_DIRECTORY_INFO *)data, cifs_sb);
740 fattr.cf_uniqueid = le64_to_cpu(
741 ((SEARCH_ID_FULL_DIR_INFO *)data)->UniqueId);
742 validinum = true;
744 cifs_buf_release(srchinf->ntwrk_buf_start);
746 kfree(srchinf);
747 } else
748 goto cgii_exit;
751 * If an inode wasn't passed in, then get the inode number
753 * Is an i_ino of zero legal? Can we use that to check if the server
754 * supports returning inode numbers? Are there other sanity checks we
755 * can use to ensure that the server is really filling in that field?
757 if (*inode == NULL) {
758 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
759 if (validinum == false) {
760 if (server->ops->get_srv_inum)
761 tmprc = server->ops->get_srv_inum(xid,
762 tcon, cifs_sb, full_path,
763 &fattr.cf_uniqueid, data);
764 if (tmprc) {
765 cifs_dbg(FYI, "GetSrvInodeNum rc %d\n",
766 tmprc);
767 fattr.cf_uniqueid = iunique(sb, ROOT_I);
768 cifs_autodisable_serverino(cifs_sb);
771 } else
772 fattr.cf_uniqueid = iunique(sb, ROOT_I);
773 } else
774 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
776 /* query for SFU type info if supported and needed */
777 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
778 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
779 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
780 if (tmprc)
781 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
784 #ifdef CONFIG_CIFS_ACL
785 /* fill in 0777 bits from ACL */
786 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
787 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, full_path, fid);
788 if (rc) {
789 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
790 __func__, rc);
791 goto cgii_exit;
794 #endif /* CONFIG_CIFS_ACL */
796 /* fill in remaining high mode bits e.g. SUID, VTX */
797 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
798 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
800 /* check for Minshall+French symlinks */
801 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
802 tmprc = CIFSCheckMFSymlink(&fattr, full_path, cifs_sb, xid);
803 if (tmprc)
804 cifs_dbg(FYI, "CIFSCheckMFSymlink: %d\n", tmprc);
807 if (!*inode) {
808 *inode = cifs_iget(sb, &fattr);
809 if (!*inode)
810 rc = -ENOMEM;
811 } else {
812 cifs_fattr_to_inode(*inode, &fattr);
815 cgii_exit:
816 kfree(buf);
817 cifs_put_tlink(tlink);
818 return rc;
821 static const struct inode_operations cifs_ipc_inode_ops = {
822 .lookup = cifs_lookup,
825 static int
826 cifs_find_inode(struct inode *inode, void *opaque)
828 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
830 /* don't match inode with different uniqueid */
831 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
832 return 0;
834 /* use createtime like an i_generation field */
835 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
836 return 0;
838 /* don't match inode of different type */
839 if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
840 return 0;
842 /* if it's not a directory or has no dentries, then flag it */
843 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
844 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
846 return 1;
849 static int
850 cifs_init_inode(struct inode *inode, void *opaque)
852 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
854 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
855 CIFS_I(inode)->createtime = fattr->cf_createtime;
856 return 0;
860 * walk dentry list for an inode and report whether it has aliases that
861 * are hashed. We use this to determine if a directory inode can actually
862 * be used.
864 static bool
865 inode_has_hashed_dentries(struct inode *inode)
867 struct dentry *dentry;
869 spin_lock(&inode->i_lock);
870 hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
871 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
872 spin_unlock(&inode->i_lock);
873 return true;
876 spin_unlock(&inode->i_lock);
877 return false;
880 /* Given fattrs, get a corresponding inode */
881 struct inode *
882 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
884 unsigned long hash;
885 struct inode *inode;
887 retry_iget5_locked:
888 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
890 /* hash down to 32-bits on 32-bit arch */
891 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
893 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
894 if (inode) {
895 /* was there a potentially problematic inode collision? */
896 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
897 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
899 if (inode_has_hashed_dentries(inode)) {
900 cifs_autodisable_serverino(CIFS_SB(sb));
901 iput(inode);
902 fattr->cf_uniqueid = iunique(sb, ROOT_I);
903 goto retry_iget5_locked;
907 cifs_fattr_to_inode(inode, fattr);
908 if (sb->s_flags & MS_NOATIME)
909 inode->i_flags |= S_NOATIME | S_NOCMTIME;
910 if (inode->i_state & I_NEW) {
911 inode->i_ino = hash;
912 if (S_ISREG(inode->i_mode))
913 inode->i_data.backing_dev_info = sb->s_bdi;
914 #ifdef CONFIG_CIFS_FSCACHE
915 /* initialize per-inode cache cookie pointer */
916 CIFS_I(inode)->fscache = NULL;
917 #endif
918 unlock_new_inode(inode);
922 return inode;
925 /* gets root inode */
926 struct inode *cifs_root_iget(struct super_block *sb)
928 unsigned int xid;
929 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
930 struct inode *inode = NULL;
931 long rc;
932 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
934 xid = get_xid();
935 if (tcon->unix_ext)
936 rc = cifs_get_inode_info_unix(&inode, "", sb, xid);
937 else
938 rc = cifs_get_inode_info(&inode, "", NULL, sb, xid, NULL);
940 if (!inode) {
941 inode = ERR_PTR(rc);
942 goto out;
945 #ifdef CONFIG_CIFS_FSCACHE
946 /* populate tcon->resource_id */
947 tcon->resource_id = CIFS_I(inode)->uniqueid;
948 #endif
950 if (rc && tcon->ipc) {
951 cifs_dbg(FYI, "ipc connection - fake read inode\n");
952 spin_lock(&inode->i_lock);
953 inode->i_mode |= S_IFDIR;
954 set_nlink(inode, 2);
955 inode->i_op = &cifs_ipc_inode_ops;
956 inode->i_fop = &simple_dir_operations;
957 inode->i_uid = cifs_sb->mnt_uid;
958 inode->i_gid = cifs_sb->mnt_gid;
959 spin_unlock(&inode->i_lock);
960 } else if (rc) {
961 iget_failed(inode);
962 inode = ERR_PTR(rc);
965 out:
966 /* can not call macro free_xid here since in a void func
967 * TODO: This is no longer true
969 _free_xid(xid);
970 return inode;
974 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
975 char *full_path, __u32 dosattr)
977 bool set_time = false;
978 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
979 struct TCP_Server_Info *server;
980 FILE_BASIC_INFO info_buf;
982 if (attrs == NULL)
983 return -EINVAL;
985 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
986 if (!server->ops->set_file_info)
987 return -ENOSYS;
989 if (attrs->ia_valid & ATTR_ATIME) {
990 set_time = true;
991 info_buf.LastAccessTime =
992 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
993 } else
994 info_buf.LastAccessTime = 0;
996 if (attrs->ia_valid & ATTR_MTIME) {
997 set_time = true;
998 info_buf.LastWriteTime =
999 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1000 } else
1001 info_buf.LastWriteTime = 0;
1004 * Samba throws this field away, but windows may actually use it.
1005 * Do not set ctime unless other time stamps are changed explicitly
1006 * (i.e. by utimes()) since we would then have a mix of client and
1007 * server times.
1009 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1010 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1011 info_buf.ChangeTime =
1012 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1013 } else
1014 info_buf.ChangeTime = 0;
1016 info_buf.CreationTime = 0; /* don't change */
1017 info_buf.Attributes = cpu_to_le32(dosattr);
1019 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1023 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1024 * and rename it to a random name that hopefully won't conflict with
1025 * anything else.
1028 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1029 const unsigned int xid)
1031 int oplock = 0;
1032 int rc;
1033 __u16 netfid;
1034 struct inode *inode = dentry->d_inode;
1035 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1036 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1037 struct tcon_link *tlink;
1038 struct cifs_tcon *tcon;
1039 __u32 dosattr, origattr;
1040 FILE_BASIC_INFO *info_buf = NULL;
1042 tlink = cifs_sb_tlink(cifs_sb);
1043 if (IS_ERR(tlink))
1044 return PTR_ERR(tlink);
1045 tcon = tlink_tcon(tlink);
1048 * We cannot rename the file if the server doesn't support
1049 * CAP_INFOLEVEL_PASSTHRU
1051 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1052 rc = -EBUSY;
1053 goto out;
1056 rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN,
1057 DELETE|FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR,
1058 &netfid, &oplock, NULL, cifs_sb->local_nls,
1059 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1060 if (rc != 0)
1061 goto out;
1063 origattr = cifsInode->cifsAttrs;
1064 if (origattr == 0)
1065 origattr |= ATTR_NORMAL;
1067 dosattr = origattr & ~ATTR_READONLY;
1068 if (dosattr == 0)
1069 dosattr |= ATTR_NORMAL;
1070 dosattr |= ATTR_HIDDEN;
1072 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1073 if (dosattr != origattr) {
1074 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1075 if (info_buf == NULL) {
1076 rc = -ENOMEM;
1077 goto out_close;
1079 info_buf->Attributes = cpu_to_le32(dosattr);
1080 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
1081 current->tgid);
1082 /* although we would like to mark the file hidden
1083 if that fails we will still try to rename it */
1084 if (!rc)
1085 cifsInode->cifsAttrs = dosattr;
1086 else
1087 dosattr = origattr; /* since not able to change them */
1090 /* rename the file */
1091 rc = CIFSSMBRenameOpenFile(xid, tcon, netfid, NULL, cifs_sb->local_nls,
1092 cifs_sb->mnt_cifs_flags &
1093 CIFS_MOUNT_MAP_SPECIAL_CHR);
1094 if (rc != 0) {
1095 rc = -EBUSY;
1096 goto undo_setattr;
1099 /* try to set DELETE_ON_CLOSE */
1100 if (!cifsInode->delete_pending) {
1101 rc = CIFSSMBSetFileDisposition(xid, tcon, true, netfid,
1102 current->tgid);
1104 * some samba versions return -ENOENT when we try to set the
1105 * file disposition here. Likely a samba bug, but work around
1106 * it for now. This means that some cifsXXX files may hang
1107 * around after they shouldn't.
1109 * BB: remove this hack after more servers have the fix
1111 if (rc == -ENOENT)
1112 rc = 0;
1113 else if (rc != 0) {
1114 rc = -EBUSY;
1115 goto undo_rename;
1117 cifsInode->delete_pending = true;
1120 out_close:
1121 CIFSSMBClose(xid, tcon, netfid);
1122 out:
1123 kfree(info_buf);
1124 cifs_put_tlink(tlink);
1125 return rc;
1128 * reset everything back to the original state. Don't bother
1129 * dealing with errors here since we can't do anything about
1130 * them anyway.
1132 undo_rename:
1133 CIFSSMBRenameOpenFile(xid, tcon, netfid, dentry->d_name.name,
1134 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1135 CIFS_MOUNT_MAP_SPECIAL_CHR);
1136 undo_setattr:
1137 if (dosattr != origattr) {
1138 info_buf->Attributes = cpu_to_le32(origattr);
1139 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
1140 current->tgid))
1141 cifsInode->cifsAttrs = origattr;
1144 goto out_close;
1147 /* copied from fs/nfs/dir.c with small changes */
1148 static void
1149 cifs_drop_nlink(struct inode *inode)
1151 spin_lock(&inode->i_lock);
1152 if (inode->i_nlink > 0)
1153 drop_nlink(inode);
1154 spin_unlock(&inode->i_lock);
1158 * If dentry->d_inode is null (usually meaning the cached dentry
1159 * is a negative dentry) then we would attempt a standard SMB delete, but
1160 * if that fails we can not attempt the fall back mechanisms on EACCESS
1161 * but will return the EACCESS to the caller. Note that the VFS does not call
1162 * unlink on negative dentries currently.
1164 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1166 int rc = 0;
1167 unsigned int xid;
1168 char *full_path = NULL;
1169 struct inode *inode = dentry->d_inode;
1170 struct cifsInodeInfo *cifs_inode;
1171 struct super_block *sb = dir->i_sb;
1172 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1173 struct tcon_link *tlink;
1174 struct cifs_tcon *tcon;
1175 struct TCP_Server_Info *server;
1176 struct iattr *attrs = NULL;
1177 __u32 dosattr = 0, origattr = 0;
1179 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1181 tlink = cifs_sb_tlink(cifs_sb);
1182 if (IS_ERR(tlink))
1183 return PTR_ERR(tlink);
1184 tcon = tlink_tcon(tlink);
1185 server = tcon->ses->server;
1187 xid = get_xid();
1189 /* Unlink can be called from rename so we can not take the
1190 * sb->s_vfs_rename_mutex here */
1191 full_path = build_path_from_dentry(dentry);
1192 if (full_path == NULL) {
1193 rc = -ENOMEM;
1194 goto unlink_out;
1197 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1198 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1199 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1200 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1201 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1202 cifs_dbg(FYI, "posix del rc %d\n", rc);
1203 if ((rc == 0) || (rc == -ENOENT))
1204 goto psx_del_no_retry;
1207 retry_std_delete:
1208 if (!server->ops->unlink) {
1209 rc = -ENOSYS;
1210 goto psx_del_no_retry;
1213 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1215 psx_del_no_retry:
1216 if (!rc) {
1217 if (inode)
1218 cifs_drop_nlink(inode);
1219 } else if (rc == -ENOENT) {
1220 d_drop(dentry);
1221 } else if (rc == -EBUSY) {
1222 if (server->ops->rename_pending_delete) {
1223 rc = server->ops->rename_pending_delete(full_path,
1224 dentry, xid);
1225 if (rc == 0)
1226 cifs_drop_nlink(inode);
1228 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1229 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1230 if (attrs == NULL) {
1231 rc = -ENOMEM;
1232 goto out_reval;
1235 /* try to reset dos attributes */
1236 cifs_inode = CIFS_I(inode);
1237 origattr = cifs_inode->cifsAttrs;
1238 if (origattr == 0)
1239 origattr |= ATTR_NORMAL;
1240 dosattr = origattr & ~ATTR_READONLY;
1241 if (dosattr == 0)
1242 dosattr |= ATTR_NORMAL;
1243 dosattr |= ATTR_HIDDEN;
1245 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1246 if (rc != 0)
1247 goto out_reval;
1249 goto retry_std_delete;
1252 /* undo the setattr if we errored out and it's needed */
1253 if (rc != 0 && dosattr != 0)
1254 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1256 out_reval:
1257 if (inode) {
1258 cifs_inode = CIFS_I(inode);
1259 cifs_inode->time = 0; /* will force revalidate to get info
1260 when needed */
1261 inode->i_ctime = current_fs_time(sb);
1263 dir->i_ctime = dir->i_mtime = current_fs_time(sb);
1264 cifs_inode = CIFS_I(dir);
1265 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1266 unlink_out:
1267 kfree(full_path);
1268 kfree(attrs);
1269 free_xid(xid);
1270 cifs_put_tlink(tlink);
1271 return rc;
1274 static int
1275 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1276 const char *full_path, struct cifs_sb_info *cifs_sb,
1277 struct cifs_tcon *tcon, const unsigned int xid)
1279 int rc = 0;
1280 struct inode *inode = NULL;
1282 if (tcon->unix_ext)
1283 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1284 xid);
1285 else
1286 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1287 xid, NULL);
1289 if (rc)
1290 return rc;
1293 * setting nlink not necessary except in cases where we failed to get it
1294 * from the server or was set bogus. Also, since this is a brand new
1295 * inode, no need to grab the i_lock before setting the i_nlink.
1297 if (inode->i_nlink < 2)
1298 set_nlink(inode, 2);
1299 mode &= ~current_umask();
1300 /* must turn on setgid bit if parent dir has it */
1301 if (parent->i_mode & S_ISGID)
1302 mode |= S_ISGID;
1304 if (tcon->unix_ext) {
1305 struct cifs_unix_set_info_args args = {
1306 .mode = mode,
1307 .ctime = NO_CHANGE_64,
1308 .atime = NO_CHANGE_64,
1309 .mtime = NO_CHANGE_64,
1310 .device = 0,
1312 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1313 args.uid = current_fsuid();
1314 if (parent->i_mode & S_ISGID)
1315 args.gid = parent->i_gid;
1316 else
1317 args.gid = current_fsgid();
1318 } else {
1319 args.uid = INVALID_UID; /* no change */
1320 args.gid = INVALID_GID; /* no change */
1322 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1323 cifs_sb->local_nls,
1324 cifs_sb->mnt_cifs_flags &
1325 CIFS_MOUNT_MAP_SPECIAL_CHR);
1326 } else {
1327 struct TCP_Server_Info *server = tcon->ses->server;
1328 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1329 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1330 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1331 tcon, xid);
1332 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1333 inode->i_mode = (mode | S_IFDIR);
1335 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1336 inode->i_uid = current_fsuid();
1337 if (inode->i_mode & S_ISGID)
1338 inode->i_gid = parent->i_gid;
1339 else
1340 inode->i_gid = current_fsgid();
1343 d_instantiate(dentry, inode);
1344 return rc;
1347 static int
1348 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1349 const char *full_path, struct cifs_sb_info *cifs_sb,
1350 struct cifs_tcon *tcon, const unsigned int xid)
1352 int rc = 0;
1353 u32 oplock = 0;
1354 FILE_UNIX_BASIC_INFO *info = NULL;
1355 struct inode *newinode = NULL;
1356 struct cifs_fattr fattr;
1358 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1359 if (info == NULL) {
1360 rc = -ENOMEM;
1361 goto posix_mkdir_out;
1364 mode &= ~current_umask();
1365 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1366 NULL /* netfid */, info, &oplock, full_path,
1367 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1368 CIFS_MOUNT_MAP_SPECIAL_CHR);
1369 if (rc == -EOPNOTSUPP)
1370 goto posix_mkdir_out;
1371 else if (rc) {
1372 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1373 d_drop(dentry);
1374 goto posix_mkdir_out;
1377 if (info->Type == cpu_to_le32(-1))
1378 /* no return info, go query for it */
1379 goto posix_mkdir_get_info;
1381 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1382 * need to set uid/gid.
1385 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1386 cifs_fill_uniqueid(inode->i_sb, &fattr);
1387 newinode = cifs_iget(inode->i_sb, &fattr);
1388 if (!newinode)
1389 goto posix_mkdir_get_info;
1391 d_instantiate(dentry, newinode);
1393 #ifdef CONFIG_CIFS_DEBUG2
1394 cifs_dbg(FYI, "instantiated dentry %p %s to inode %p\n",
1395 dentry, dentry->d_name.name, newinode);
1397 if (newinode->i_nlink != 2)
1398 cifs_dbg(FYI, "unexpected number of links %d\n",
1399 newinode->i_nlink);
1400 #endif
1402 posix_mkdir_out:
1403 kfree(info);
1404 return rc;
1405 posix_mkdir_get_info:
1406 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1407 xid);
1408 goto posix_mkdir_out;
1411 int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
1413 int rc = 0;
1414 unsigned int xid;
1415 struct cifs_sb_info *cifs_sb;
1416 struct tcon_link *tlink;
1417 struct cifs_tcon *tcon;
1418 struct TCP_Server_Info *server;
1419 char *full_path;
1421 cifs_dbg(FYI, "In cifs_mkdir, mode = 0x%hx inode = 0x%p\n",
1422 mode, inode);
1424 cifs_sb = CIFS_SB(inode->i_sb);
1425 tlink = cifs_sb_tlink(cifs_sb);
1426 if (IS_ERR(tlink))
1427 return PTR_ERR(tlink);
1428 tcon = tlink_tcon(tlink);
1430 xid = get_xid();
1432 full_path = build_path_from_dentry(direntry);
1433 if (full_path == NULL) {
1434 rc = -ENOMEM;
1435 goto mkdir_out;
1438 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1439 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1440 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1441 tcon, xid);
1442 if (rc != -EOPNOTSUPP)
1443 goto mkdir_out;
1446 server = tcon->ses->server;
1448 if (!server->ops->mkdir) {
1449 rc = -ENOSYS;
1450 goto mkdir_out;
1453 /* BB add setting the equivalent of mode via CreateX w/ACLs */
1454 rc = server->ops->mkdir(xid, tcon, full_path, cifs_sb);
1455 if (rc) {
1456 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1457 d_drop(direntry);
1458 goto mkdir_out;
1461 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1462 xid);
1463 mkdir_out:
1465 * Force revalidate to get parent dir info when needed since cached
1466 * attributes are invalid now.
1468 CIFS_I(inode)->time = 0;
1469 kfree(full_path);
1470 free_xid(xid);
1471 cifs_put_tlink(tlink);
1472 return rc;
1475 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1477 int rc = 0;
1478 unsigned int xid;
1479 struct cifs_sb_info *cifs_sb;
1480 struct tcon_link *tlink;
1481 struct cifs_tcon *tcon;
1482 struct TCP_Server_Info *server;
1483 char *full_path = NULL;
1484 struct cifsInodeInfo *cifsInode;
1486 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
1488 xid = get_xid();
1490 full_path = build_path_from_dentry(direntry);
1491 if (full_path == NULL) {
1492 rc = -ENOMEM;
1493 goto rmdir_exit;
1496 cifs_sb = CIFS_SB(inode->i_sb);
1497 tlink = cifs_sb_tlink(cifs_sb);
1498 if (IS_ERR(tlink)) {
1499 rc = PTR_ERR(tlink);
1500 goto rmdir_exit;
1502 tcon = tlink_tcon(tlink);
1503 server = tcon->ses->server;
1505 if (!server->ops->rmdir) {
1506 rc = -ENOSYS;
1507 cifs_put_tlink(tlink);
1508 goto rmdir_exit;
1511 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
1512 cifs_put_tlink(tlink);
1514 if (!rc) {
1515 spin_lock(&direntry->d_inode->i_lock);
1516 i_size_write(direntry->d_inode, 0);
1517 clear_nlink(direntry->d_inode);
1518 spin_unlock(&direntry->d_inode->i_lock);
1521 cifsInode = CIFS_I(direntry->d_inode);
1522 /* force revalidate to go get info when needed */
1523 cifsInode->time = 0;
1525 cifsInode = CIFS_I(inode);
1527 * Force revalidate to get parent dir info when needed since cached
1528 * attributes are invalid now.
1530 cifsInode->time = 0;
1532 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
1533 current_fs_time(inode->i_sb);
1535 rmdir_exit:
1536 kfree(full_path);
1537 free_xid(xid);
1538 return rc;
1541 static int
1542 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
1543 const char *from_path, struct dentry *to_dentry,
1544 const char *to_path)
1546 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
1547 struct tcon_link *tlink;
1548 struct cifs_tcon *tcon;
1549 struct TCP_Server_Info *server;
1550 __u16 srcfid;
1551 int oplock, rc;
1553 tlink = cifs_sb_tlink(cifs_sb);
1554 if (IS_ERR(tlink))
1555 return PTR_ERR(tlink);
1556 tcon = tlink_tcon(tlink);
1557 server = tcon->ses->server;
1559 if (!server->ops->rename)
1560 return -ENOSYS;
1562 /* try path-based rename first */
1563 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
1566 * Don't bother with rename by filehandle unless file is busy and
1567 * source. Note that cross directory moves do not work with
1568 * rename by filehandle to various Windows servers.
1570 if (rc == 0 || rc != -EBUSY)
1571 goto do_rename_exit;
1573 /* open-file renames don't work across directories */
1574 if (to_dentry->d_parent != from_dentry->d_parent)
1575 goto do_rename_exit;
1577 /* open the file to be renamed -- we need DELETE perms */
1578 rc = CIFSSMBOpen(xid, tcon, from_path, FILE_OPEN, DELETE,
1579 CREATE_NOT_DIR, &srcfid, &oplock, NULL,
1580 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1581 CIFS_MOUNT_MAP_SPECIAL_CHR);
1582 if (rc == 0) {
1583 rc = CIFSSMBRenameOpenFile(xid, tcon, srcfid,
1584 (const char *) to_dentry->d_name.name,
1585 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1586 CIFS_MOUNT_MAP_SPECIAL_CHR);
1587 CIFSSMBClose(xid, tcon, srcfid);
1589 do_rename_exit:
1590 cifs_put_tlink(tlink);
1591 return rc;
1595 cifs_rename(struct inode *source_dir, struct dentry *source_dentry,
1596 struct inode *target_dir, struct dentry *target_dentry)
1598 char *from_name = NULL;
1599 char *to_name = NULL;
1600 struct cifs_sb_info *cifs_sb;
1601 struct tcon_link *tlink;
1602 struct cifs_tcon *tcon;
1603 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
1604 FILE_UNIX_BASIC_INFO *info_buf_target;
1605 unsigned int xid;
1606 int rc, tmprc;
1608 cifs_sb = CIFS_SB(source_dir->i_sb);
1609 tlink = cifs_sb_tlink(cifs_sb);
1610 if (IS_ERR(tlink))
1611 return PTR_ERR(tlink);
1612 tcon = tlink_tcon(tlink);
1614 xid = get_xid();
1617 * we already have the rename sem so we do not need to
1618 * grab it again here to protect the path integrity
1620 from_name = build_path_from_dentry(source_dentry);
1621 if (from_name == NULL) {
1622 rc = -ENOMEM;
1623 goto cifs_rename_exit;
1626 to_name = build_path_from_dentry(target_dentry);
1627 if (to_name == NULL) {
1628 rc = -ENOMEM;
1629 goto cifs_rename_exit;
1632 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
1633 to_name);
1635 if (rc == -EEXIST && tcon->unix_ext) {
1637 * Are src and dst hardlinks of same inode? We can only tell
1638 * with unix extensions enabled.
1640 info_buf_source =
1641 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
1642 GFP_KERNEL);
1643 if (info_buf_source == NULL) {
1644 rc = -ENOMEM;
1645 goto cifs_rename_exit;
1648 info_buf_target = info_buf_source + 1;
1649 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
1650 info_buf_source,
1651 cifs_sb->local_nls,
1652 cifs_sb->mnt_cifs_flags &
1653 CIFS_MOUNT_MAP_SPECIAL_CHR);
1654 if (tmprc != 0)
1655 goto unlink_target;
1657 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
1658 info_buf_target,
1659 cifs_sb->local_nls,
1660 cifs_sb->mnt_cifs_flags &
1661 CIFS_MOUNT_MAP_SPECIAL_CHR);
1663 if (tmprc == 0 && (info_buf_source->UniqueId ==
1664 info_buf_target->UniqueId)) {
1665 /* same file, POSIX says that this is a noop */
1666 rc = 0;
1667 goto cifs_rename_exit;
1671 * else ... BB we could add the same check for Windows by
1672 * checking the UniqueId via FILE_INTERNAL_INFO
1675 unlink_target:
1676 /* Try unlinking the target dentry if it's not negative */
1677 if (target_dentry->d_inode && (rc == -EACCES || rc == -EEXIST)) {
1678 tmprc = cifs_unlink(target_dir, target_dentry);
1679 if (tmprc)
1680 goto cifs_rename_exit;
1681 rc = cifs_do_rename(xid, source_dentry, from_name,
1682 target_dentry, to_name);
1685 cifs_rename_exit:
1686 kfree(info_buf_source);
1687 kfree(from_name);
1688 kfree(to_name);
1689 free_xid(xid);
1690 cifs_put_tlink(tlink);
1691 return rc;
1694 static bool
1695 cifs_inode_needs_reval(struct inode *inode)
1697 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
1698 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1700 if (CIFS_CACHE_READ(cifs_i))
1701 return false;
1703 if (!lookupCacheEnabled)
1704 return true;
1706 if (cifs_i->time == 0)
1707 return true;
1709 if (!time_in_range(jiffies, cifs_i->time,
1710 cifs_i->time + cifs_sb->actimeo))
1711 return true;
1713 /* hardlinked files w/ noserverino get "special" treatment */
1714 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
1715 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
1716 return true;
1718 return false;
1722 * Zap the cache. Called when invalid_mapping flag is set.
1725 cifs_invalidate_mapping(struct inode *inode)
1727 int rc = 0;
1728 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
1730 cifs_i->invalid_mapping = false;
1732 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
1733 rc = invalidate_inode_pages2(inode->i_mapping);
1734 if (rc) {
1735 cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
1736 __func__, inode);
1737 cifs_i->invalid_mapping = true;
1741 cifs_fscache_reset_inode_cookie(inode);
1742 return rc;
1745 int cifs_revalidate_file_attr(struct file *filp)
1747 int rc = 0;
1748 struct inode *inode = file_inode(filp);
1749 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
1751 if (!cifs_inode_needs_reval(inode))
1752 return rc;
1754 if (tlink_tcon(cfile->tlink)->unix_ext)
1755 rc = cifs_get_file_info_unix(filp);
1756 else
1757 rc = cifs_get_file_info(filp);
1759 return rc;
1762 int cifs_revalidate_dentry_attr(struct dentry *dentry)
1764 unsigned int xid;
1765 int rc = 0;
1766 struct inode *inode = dentry->d_inode;
1767 struct super_block *sb = dentry->d_sb;
1768 char *full_path = NULL;
1770 if (inode == NULL)
1771 return -ENOENT;
1773 if (!cifs_inode_needs_reval(inode))
1774 return rc;
1776 xid = get_xid();
1778 /* can not safely grab the rename sem here if rename calls revalidate
1779 since that would deadlock */
1780 full_path = build_path_from_dentry(dentry);
1781 if (full_path == NULL) {
1782 rc = -ENOMEM;
1783 goto out;
1786 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
1787 full_path, inode, inode->i_count.counter,
1788 dentry, dentry->d_time, jiffies);
1790 if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
1791 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
1792 else
1793 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
1794 xid, NULL);
1796 out:
1797 kfree(full_path);
1798 free_xid(xid);
1799 return rc;
1802 int cifs_revalidate_file(struct file *filp)
1804 int rc;
1805 struct inode *inode = file_inode(filp);
1807 rc = cifs_revalidate_file_attr(filp);
1808 if (rc)
1809 return rc;
1811 if (CIFS_I(inode)->invalid_mapping)
1812 rc = cifs_invalidate_mapping(inode);
1813 return rc;
1816 /* revalidate a dentry's inode attributes */
1817 int cifs_revalidate_dentry(struct dentry *dentry)
1819 int rc;
1820 struct inode *inode = dentry->d_inode;
1822 rc = cifs_revalidate_dentry_attr(dentry);
1823 if (rc)
1824 return rc;
1826 if (CIFS_I(inode)->invalid_mapping)
1827 rc = cifs_invalidate_mapping(inode);
1828 return rc;
1831 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1832 struct kstat *stat)
1834 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
1835 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1836 struct inode *inode = dentry->d_inode;
1837 int rc;
1840 * We need to be sure that all dirty pages are written and the server
1841 * has actual ctime, mtime and file length.
1843 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
1844 inode->i_mapping->nrpages != 0) {
1845 rc = filemap_fdatawait(inode->i_mapping);
1846 if (rc) {
1847 mapping_set_error(inode->i_mapping, rc);
1848 return rc;
1852 rc = cifs_revalidate_dentry_attr(dentry);
1853 if (rc)
1854 return rc;
1856 generic_fillattr(inode, stat);
1857 stat->blksize = CIFS_MAX_MSGSIZE;
1858 stat->ino = CIFS_I(inode)->uniqueid;
1861 * If on a multiuser mount without unix extensions or cifsacl being
1862 * enabled, and the admin hasn't overridden them, set the ownership
1863 * to the fsuid/fsgid of the current process.
1865 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
1866 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1867 !tcon->unix_ext) {
1868 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
1869 stat->uid = current_fsuid();
1870 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
1871 stat->gid = current_fsgid();
1873 return rc;
1876 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1878 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1879 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1880 struct page *page;
1881 int rc = 0;
1883 page = grab_cache_page(mapping, index);
1884 if (!page)
1885 return -ENOMEM;
1887 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
1888 unlock_page(page);
1889 page_cache_release(page);
1890 return rc;
1893 static void cifs_setsize(struct inode *inode, loff_t offset)
1895 spin_lock(&inode->i_lock);
1896 i_size_write(inode, offset);
1897 spin_unlock(&inode->i_lock);
1899 truncate_pagecache(inode, offset);
1902 static int
1903 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
1904 unsigned int xid, char *full_path)
1906 int rc;
1907 struct cifsFileInfo *open_file;
1908 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1909 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1910 struct tcon_link *tlink = NULL;
1911 struct cifs_tcon *tcon = NULL;
1912 struct TCP_Server_Info *server;
1913 struct cifs_io_parms io_parms;
1916 * To avoid spurious oplock breaks from server, in the case of
1917 * inodes that we already have open, avoid doing path based
1918 * setting of file size if we can do it by handle.
1919 * This keeps our caching token (oplock) and avoids timeouts
1920 * when the local oplock break takes longer to flush
1921 * writebehind data than the SMB timeout for the SetPathInfo
1922 * request would allow
1924 open_file = find_writable_file(cifsInode, true);
1925 if (open_file) {
1926 tcon = tlink_tcon(open_file->tlink);
1927 server = tcon->ses->server;
1928 if (server->ops->set_file_size)
1929 rc = server->ops->set_file_size(xid, tcon, open_file,
1930 attrs->ia_size, false);
1931 else
1932 rc = -ENOSYS;
1933 cifsFileInfo_put(open_file);
1934 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
1935 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1936 unsigned int bytes_written;
1938 io_parms.netfid = open_file->fid.netfid;
1939 io_parms.pid = open_file->pid;
1940 io_parms.tcon = tcon;
1941 io_parms.offset = 0;
1942 io_parms.length = attrs->ia_size;
1943 rc = CIFSSMBWrite(xid, &io_parms, &bytes_written,
1944 NULL, NULL, 1);
1945 cifs_dbg(FYI, "Wrt seteof rc %d\n", rc);
1947 } else
1948 rc = -EINVAL;
1950 if (!rc)
1951 goto set_size_out;
1953 if (tcon == NULL) {
1954 tlink = cifs_sb_tlink(cifs_sb);
1955 if (IS_ERR(tlink))
1956 return PTR_ERR(tlink);
1957 tcon = tlink_tcon(tlink);
1958 server = tcon->ses->server;
1962 * Set file size by pathname rather than by handle either because no
1963 * valid, writeable file handle for it was found or because there was
1964 * an error setting it by handle.
1966 if (server->ops->set_path_size)
1967 rc = server->ops->set_path_size(xid, tcon, full_path,
1968 attrs->ia_size, cifs_sb, false);
1969 else
1970 rc = -ENOSYS;
1971 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
1972 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1973 __u16 netfid;
1974 int oplock = 0;
1976 rc = SMBLegacyOpen(xid, tcon, full_path, FILE_OPEN,
1977 GENERIC_WRITE, CREATE_NOT_DIR, &netfid,
1978 &oplock, NULL, cifs_sb->local_nls,
1979 cifs_sb->mnt_cifs_flags &
1980 CIFS_MOUNT_MAP_SPECIAL_CHR);
1981 if (rc == 0) {
1982 unsigned int bytes_written;
1984 io_parms.netfid = netfid;
1985 io_parms.pid = current->tgid;
1986 io_parms.tcon = tcon;
1987 io_parms.offset = 0;
1988 io_parms.length = attrs->ia_size;
1989 rc = CIFSSMBWrite(xid, &io_parms, &bytes_written, NULL,
1990 NULL, 1);
1991 cifs_dbg(FYI, "wrt seteof rc %d\n", rc);
1992 CIFSSMBClose(xid, tcon, netfid);
1995 if (tlink)
1996 cifs_put_tlink(tlink);
1998 set_size_out:
1999 if (rc == 0) {
2000 cifsInode->server_eof = attrs->ia_size;
2001 cifs_setsize(inode, attrs->ia_size);
2002 cifs_truncate_page(inode->i_mapping, inode->i_size);
2005 return rc;
2008 static int
2009 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2011 int rc;
2012 unsigned int xid;
2013 char *full_path = NULL;
2014 struct inode *inode = direntry->d_inode;
2015 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2016 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2017 struct tcon_link *tlink;
2018 struct cifs_tcon *pTcon;
2019 struct cifs_unix_set_info_args *args = NULL;
2020 struct cifsFileInfo *open_file;
2022 cifs_dbg(FYI, "setattr_unix on file %s attrs->ia_valid=0x%x\n",
2023 direntry->d_name.name, attrs->ia_valid);
2025 xid = get_xid();
2027 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2028 attrs->ia_valid |= ATTR_FORCE;
2030 rc = inode_change_ok(inode, attrs);
2031 if (rc < 0)
2032 goto out;
2034 full_path = build_path_from_dentry(direntry);
2035 if (full_path == NULL) {
2036 rc = -ENOMEM;
2037 goto out;
2041 * Attempt to flush data before changing attributes. We need to do
2042 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2043 * ownership or mode then we may also need to do this. Here, we take
2044 * the safe way out and just do the flush on all setattr requests. If
2045 * the flush returns error, store it to report later and continue.
2047 * BB: This should be smarter. Why bother flushing pages that
2048 * will be truncated anyway? Also, should we error out here if
2049 * the flush returns error?
2051 rc = filemap_write_and_wait(inode->i_mapping);
2052 mapping_set_error(inode->i_mapping, rc);
2053 rc = 0;
2055 if (attrs->ia_valid & ATTR_SIZE) {
2056 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2057 if (rc != 0)
2058 goto out;
2061 /* skip mode change if it's just for clearing setuid/setgid */
2062 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2063 attrs->ia_valid &= ~ATTR_MODE;
2065 args = kmalloc(sizeof(*args), GFP_KERNEL);
2066 if (args == NULL) {
2067 rc = -ENOMEM;
2068 goto out;
2071 /* set up the struct */
2072 if (attrs->ia_valid & ATTR_MODE)
2073 args->mode = attrs->ia_mode;
2074 else
2075 args->mode = NO_CHANGE_64;
2077 if (attrs->ia_valid & ATTR_UID)
2078 args->uid = attrs->ia_uid;
2079 else
2080 args->uid = INVALID_UID; /* no change */
2082 if (attrs->ia_valid & ATTR_GID)
2083 args->gid = attrs->ia_gid;
2084 else
2085 args->gid = INVALID_GID; /* no change */
2087 if (attrs->ia_valid & ATTR_ATIME)
2088 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2089 else
2090 args->atime = NO_CHANGE_64;
2092 if (attrs->ia_valid & ATTR_MTIME)
2093 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2094 else
2095 args->mtime = NO_CHANGE_64;
2097 if (attrs->ia_valid & ATTR_CTIME)
2098 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2099 else
2100 args->ctime = NO_CHANGE_64;
2102 args->device = 0;
2103 open_file = find_writable_file(cifsInode, true);
2104 if (open_file) {
2105 u16 nfid = open_file->fid.netfid;
2106 u32 npid = open_file->pid;
2107 pTcon = tlink_tcon(open_file->tlink);
2108 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2109 cifsFileInfo_put(open_file);
2110 } else {
2111 tlink = cifs_sb_tlink(cifs_sb);
2112 if (IS_ERR(tlink)) {
2113 rc = PTR_ERR(tlink);
2114 goto out;
2116 pTcon = tlink_tcon(tlink);
2117 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2118 cifs_sb->local_nls,
2119 cifs_sb->mnt_cifs_flags &
2120 CIFS_MOUNT_MAP_SPECIAL_CHR);
2121 cifs_put_tlink(tlink);
2124 if (rc)
2125 goto out;
2127 if ((attrs->ia_valid & ATTR_SIZE) &&
2128 attrs->ia_size != i_size_read(inode))
2129 truncate_setsize(inode, attrs->ia_size);
2131 setattr_copy(inode, attrs);
2132 mark_inode_dirty(inode);
2134 /* force revalidate when any of these times are set since some
2135 of the fs types (eg ext3, fat) do not have fine enough
2136 time granularity to match protocol, and we do not have a
2137 a way (yet) to query the server fs's time granularity (and
2138 whether it rounds times down).
2140 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2141 cifsInode->time = 0;
2142 out:
2143 kfree(args);
2144 kfree(full_path);
2145 free_xid(xid);
2146 return rc;
2149 static int
2150 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2152 unsigned int xid;
2153 kuid_t uid = INVALID_UID;
2154 kgid_t gid = INVALID_GID;
2155 struct inode *inode = direntry->d_inode;
2156 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2157 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2158 char *full_path = NULL;
2159 int rc = -EACCES;
2160 __u32 dosattr = 0;
2161 __u64 mode = NO_CHANGE_64;
2163 xid = get_xid();
2165 cifs_dbg(FYI, "setattr on file %s attrs->iavalid 0x%x\n",
2166 direntry->d_name.name, attrs->ia_valid);
2168 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2169 attrs->ia_valid |= ATTR_FORCE;
2171 rc = inode_change_ok(inode, attrs);
2172 if (rc < 0) {
2173 free_xid(xid);
2174 return rc;
2177 full_path = build_path_from_dentry(direntry);
2178 if (full_path == NULL) {
2179 rc = -ENOMEM;
2180 free_xid(xid);
2181 return rc;
2185 * Attempt to flush data before changing attributes. We need to do
2186 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2187 * ownership or mode then we may also need to do this. Here, we take
2188 * the safe way out and just do the flush on all setattr requests. If
2189 * the flush returns error, store it to report later and continue.
2191 * BB: This should be smarter. Why bother flushing pages that
2192 * will be truncated anyway? Also, should we error out here if
2193 * the flush returns error?
2195 rc = filemap_write_and_wait(inode->i_mapping);
2196 mapping_set_error(inode->i_mapping, rc);
2197 rc = 0;
2199 if (attrs->ia_valid & ATTR_SIZE) {
2200 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2201 if (rc != 0)
2202 goto cifs_setattr_exit;
2205 if (attrs->ia_valid & ATTR_UID)
2206 uid = attrs->ia_uid;
2208 if (attrs->ia_valid & ATTR_GID)
2209 gid = attrs->ia_gid;
2211 #ifdef CONFIG_CIFS_ACL
2212 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
2213 if (uid_valid(uid) || gid_valid(gid)) {
2214 rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
2215 uid, gid);
2216 if (rc) {
2217 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2218 __func__, rc);
2219 goto cifs_setattr_exit;
2222 } else
2223 #endif /* CONFIG_CIFS_ACL */
2224 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2225 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2227 /* skip mode change if it's just for clearing setuid/setgid */
2228 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2229 attrs->ia_valid &= ~ATTR_MODE;
2231 if (attrs->ia_valid & ATTR_MODE) {
2232 mode = attrs->ia_mode;
2233 rc = 0;
2234 #ifdef CONFIG_CIFS_ACL
2235 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
2236 rc = id_mode_to_cifs_acl(inode, full_path, mode,
2237 INVALID_UID, INVALID_GID);
2238 if (rc) {
2239 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2240 __func__, rc);
2241 goto cifs_setattr_exit;
2243 } else
2244 #endif /* CONFIG_CIFS_ACL */
2245 if (((mode & S_IWUGO) == 0) &&
2246 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
2248 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2250 /* fix up mode if we're not using dynperm */
2251 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2252 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2253 } else if ((mode & S_IWUGO) &&
2254 (cifsInode->cifsAttrs & ATTR_READONLY)) {
2256 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2257 /* Attributes of 0 are ignored */
2258 if (dosattr == 0)
2259 dosattr |= ATTR_NORMAL;
2261 /* reset local inode permissions to normal */
2262 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2263 attrs->ia_mode &= ~(S_IALLUGO);
2264 if (S_ISDIR(inode->i_mode))
2265 attrs->ia_mode |=
2266 cifs_sb->mnt_dir_mode;
2267 else
2268 attrs->ia_mode |=
2269 cifs_sb->mnt_file_mode;
2271 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2272 /* ignore mode change - ATTR_READONLY hasn't changed */
2273 attrs->ia_valid &= ~ATTR_MODE;
2277 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2278 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2279 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2280 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
2282 /* Even if error on time set, no sense failing the call if
2283 the server would set the time to a reasonable value anyway,
2284 and this check ensures that we are not being called from
2285 sys_utimes in which case we ought to fail the call back to
2286 the user when the server rejects the call */
2287 if ((rc) && (attrs->ia_valid &
2288 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
2289 rc = 0;
2292 /* do not need local check to inode_check_ok since the server does
2293 that */
2294 if (rc)
2295 goto cifs_setattr_exit;
2297 if ((attrs->ia_valid & ATTR_SIZE) &&
2298 attrs->ia_size != i_size_read(inode))
2299 truncate_setsize(inode, attrs->ia_size);
2301 setattr_copy(inode, attrs);
2302 mark_inode_dirty(inode);
2304 cifs_setattr_exit:
2305 kfree(full_path);
2306 free_xid(xid);
2307 return rc;
2311 cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2313 struct inode *inode = direntry->d_inode;
2314 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2315 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
2317 if (pTcon->unix_ext)
2318 return cifs_setattr_unix(direntry, attrs);
2320 return cifs_setattr_nounix(direntry, attrs);
2322 /* BB: add cifs_setattr_legacy for really old servers */
2325 #if 0
2326 void cifs_delete_inode(struct inode *inode)
2328 cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
2329 /* may have to add back in if and when safe distributed caching of
2330 directories added e.g. via FindNotify */
2332 #endif