Merge tag 'sched-core-2022-10-07' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / fs / cifs / inode.c
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2010
5  *   Author(s): Steve French (sfrench@us.ibm.com)
6  *
7  */
8 #include <linux/fs.h>
9 #include <linux/stat.h>
10 #include <linux/slab.h>
11 #include <linux/pagemap.h>
12 #include <linux/freezer.h>
13 #include <linux/sched/signal.h>
14 #include <linux/wait_bit.h>
15 #include <linux/fiemap.h>
16 #include <asm/div64.h>
17 #include "cifsfs.h"
18 #include "cifspdu.h"
19 #include "cifsglob.h"
20 #include "cifsproto.h"
21 #include "smb2proto.h"
22 #include "cifs_debug.h"
23 #include "cifs_fs_sb.h"
24 #include "cifs_unicode.h"
25 #include "fscache.h"
26 #include "fs_context.h"
27 #include "cifs_ioctl.h"
28 #include "cached_dir.h"
29
30 static void cifs_set_ops(struct inode *inode)
31 {
32         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
33
34         switch (inode->i_mode & S_IFMT) {
35         case S_IFREG:
36                 inode->i_op = &cifs_file_inode_ops;
37                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
38                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
39                                 inode->i_fop = &cifs_file_direct_nobrl_ops;
40                         else
41                                 inode->i_fop = &cifs_file_direct_ops;
42                 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
43                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
44                                 inode->i_fop = &cifs_file_strict_nobrl_ops;
45                         else
46                                 inode->i_fop = &cifs_file_strict_ops;
47                 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
48                         inode->i_fop = &cifs_file_nobrl_ops;
49                 else { /* not direct, send byte range locks */
50                         inode->i_fop = &cifs_file_ops;
51                 }
52
53                 /* check if server can support readahead */
54                 if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
55                                 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
56                         inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
57                 else
58                         inode->i_data.a_ops = &cifs_addr_ops;
59                 break;
60         case S_IFDIR:
61 #ifdef CONFIG_CIFS_DFS_UPCALL
62                 if (IS_AUTOMOUNT(inode)) {
63                         inode->i_op = &cifs_dfs_referral_inode_operations;
64                 } else {
65 #else /* NO DFS support, treat as a directory */
66                 {
67 #endif
68                         inode->i_op = &cifs_dir_inode_ops;
69                         inode->i_fop = &cifs_dir_ops;
70                 }
71                 break;
72         case S_IFLNK:
73                 inode->i_op = &cifs_symlink_inode_ops;
74                 break;
75         default:
76                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
77                 break;
78         }
79 }
80
81 /* check inode attributes against fattr. If they don't match, tag the
82  * inode for cache invalidation
83  */
84 static void
85 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
86 {
87         struct cifs_fscache_inode_coherency_data cd;
88         struct cifsInodeInfo *cifs_i = CIFS_I(inode);
89
90         cifs_dbg(FYI, "%s: revalidating inode %llu\n",
91                  __func__, cifs_i->uniqueid);
92
93         if (inode->i_state & I_NEW) {
94                 cifs_dbg(FYI, "%s: inode %llu is new\n",
95                          __func__, cifs_i->uniqueid);
96                 return;
97         }
98
99         /* don't bother with revalidation if we have an oplock */
100         if (CIFS_CACHE_READ(cifs_i)) {
101                 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
102                          __func__, cifs_i->uniqueid);
103                 return;
104         }
105
106          /* revalidate if mtime or size have changed */
107         fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
108         if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) &&
109             cifs_i->server_eof == fattr->cf_eof) {
110                 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
111                          __func__, cifs_i->uniqueid);
112                 return;
113         }
114
115         cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
116                  __func__, cifs_i->uniqueid);
117         set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
118         /* Invalidate fscache cookie */
119         cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
120         fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
121 }
122
123 /*
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
126  */
127 static void
128 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
129 {
130         /*
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
134          */
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);
142                 }
143                 return;
144         }
145
146         /* we trust the server, so update it */
147         set_nlink(inode, fattr->cf_nlink);
148 }
149
150 /* populate an inode with info from a cifs_fattr struct */
151 int
152 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
153 {
154         struct cifsInodeInfo *cifs_i = CIFS_I(inode);
155         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
156
157         if (!(inode->i_state & I_NEW) &&
158             unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
159                 CIFS_I(inode)->time = 0; /* force reval */
160                 return -ESTALE;
161         }
162
163         cifs_revalidate_cache(inode, fattr);
164
165         spin_lock(&inode->i_lock);
166         fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
167         fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
168         fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
169         /* we do not want atime to be less than mtime, it broke some apps */
170         if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
171                 inode->i_atime = fattr->cf_mtime;
172         else
173                 inode->i_atime = fattr->cf_atime;
174         inode->i_mtime = fattr->cf_mtime;
175         inode->i_ctime = fattr->cf_ctime;
176         inode->i_rdev = fattr->cf_rdev;
177         cifs_nlink_fattr_to_inode(inode, fattr);
178         inode->i_uid = fattr->cf_uid;
179         inode->i_gid = fattr->cf_gid;
180
181         /* if dynperm is set, don't clobber existing mode */
182         if (inode->i_state & I_NEW ||
183             !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
184                 inode->i_mode = fattr->cf_mode;
185
186         cifs_i->cifsAttrs = fattr->cf_cifsattrs;
187
188         if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
189                 cifs_i->time = 0;
190         else
191                 cifs_i->time = jiffies;
192
193         if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
194                 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
195         else
196                 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
197
198         cifs_i->server_eof = fattr->cf_eof;
199         /*
200          * Can't safely change the file size here if the client is writing to
201          * it due to potential races.
202          */
203         if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
204                 i_size_write(inode, fattr->cf_eof);
205
206                 /*
207                  * i_blocks is not related to (i_size / i_blksize),
208                  * but instead 512 byte (2**9) size is required for
209                  * calculating num blocks.
210                  */
211                 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
212         }
213         spin_unlock(&inode->i_lock);
214
215         if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
216                 inode->i_flags |= S_AUTOMOUNT;
217         if (inode->i_state & I_NEW)
218                 cifs_set_ops(inode);
219         return 0;
220 }
221
222 void
223 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
224 {
225         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
226
227         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
228                 return;
229
230         fattr->cf_uniqueid = iunique(sb, ROOT_I);
231 }
232
233 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
234 void
235 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
236                          struct cifs_sb_info *cifs_sb)
237 {
238         memset(fattr, 0, sizeof(*fattr));
239         fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
240         fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
241         fattr->cf_eof = le64_to_cpu(info->EndOfFile);
242
243         fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
244         fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
245         fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
246         /* old POSIX extensions don't get create time */
247
248         fattr->cf_mode = le64_to_cpu(info->Permissions);
249
250         /*
251          * Since we set the inode type below we need to mask off
252          * to avoid strange results if bits set above.
253          */
254         fattr->cf_mode &= ~S_IFMT;
255         switch (le32_to_cpu(info->Type)) {
256         case UNIX_FILE:
257                 fattr->cf_mode |= S_IFREG;
258                 fattr->cf_dtype = DT_REG;
259                 break;
260         case UNIX_SYMLINK:
261                 fattr->cf_mode |= S_IFLNK;
262                 fattr->cf_dtype = DT_LNK;
263                 break;
264         case UNIX_DIR:
265                 fattr->cf_mode |= S_IFDIR;
266                 fattr->cf_dtype = DT_DIR;
267                 break;
268         case UNIX_CHARDEV:
269                 fattr->cf_mode |= S_IFCHR;
270                 fattr->cf_dtype = DT_CHR;
271                 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
272                                        le64_to_cpu(info->DevMinor) & MINORMASK);
273                 break;
274         case UNIX_BLOCKDEV:
275                 fattr->cf_mode |= S_IFBLK;
276                 fattr->cf_dtype = DT_BLK;
277                 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
278                                        le64_to_cpu(info->DevMinor) & MINORMASK);
279                 break;
280         case UNIX_FIFO:
281                 fattr->cf_mode |= S_IFIFO;
282                 fattr->cf_dtype = DT_FIFO;
283                 break;
284         case UNIX_SOCKET:
285                 fattr->cf_mode |= S_IFSOCK;
286                 fattr->cf_dtype = DT_SOCK;
287                 break;
288         default:
289                 /* safest to call it a file if we do not know */
290                 fattr->cf_mode |= S_IFREG;
291                 fattr->cf_dtype = DT_REG;
292                 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
293                 break;
294         }
295
296         fattr->cf_uid = cifs_sb->ctx->linux_uid;
297         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
298                 u64 id = le64_to_cpu(info->Uid);
299                 if (id < ((uid_t)-1)) {
300                         kuid_t uid = make_kuid(&init_user_ns, id);
301                         if (uid_valid(uid))
302                                 fattr->cf_uid = uid;
303                 }
304         }
305         
306         fattr->cf_gid = cifs_sb->ctx->linux_gid;
307         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
308                 u64 id = le64_to_cpu(info->Gid);
309                 if (id < ((gid_t)-1)) {
310                         kgid_t gid = make_kgid(&init_user_ns, id);
311                         if (gid_valid(gid))
312                                 fattr->cf_gid = gid;
313                 }
314         }
315
316         fattr->cf_nlink = le64_to_cpu(info->Nlinks);
317 }
318
319 /*
320  * Fill a cifs_fattr struct with fake inode info.
321  *
322  * Needed to setup cifs_fattr data for the directory which is the
323  * junction to the new submount (ie to setup the fake directory
324  * which represents a DFS referral).
325  */
326 static void
327 cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
328 {
329         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
330
331         cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
332
333         memset(fattr, 0, sizeof(*fattr));
334         fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
335         fattr->cf_uid = cifs_sb->ctx->linux_uid;
336         fattr->cf_gid = cifs_sb->ctx->linux_gid;
337         ktime_get_coarse_real_ts64(&fattr->cf_mtime);
338         fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
339         fattr->cf_nlink = 2;
340         fattr->cf_flags = CIFS_FATTR_DFS_REFERRAL;
341 }
342
343 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
344 static int
345 cifs_get_file_info_unix(struct file *filp)
346 {
347         int rc;
348         unsigned int xid;
349         FILE_UNIX_BASIC_INFO find_data;
350         struct cifs_fattr fattr;
351         struct inode *inode = file_inode(filp);
352         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
353         struct cifsFileInfo *cfile = filp->private_data;
354         struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
355
356         xid = get_xid();
357         rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
358         if (!rc) {
359                 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
360         } else if (rc == -EREMOTE) {
361                 cifs_create_dfs_fattr(&fattr, inode->i_sb);
362                 rc = 0;
363         } else
364                 goto cifs_gfiunix_out;
365
366         rc = cifs_fattr_to_inode(inode, &fattr);
367
368 cifs_gfiunix_out:
369         free_xid(xid);
370         return rc;
371 }
372
373 int cifs_get_inode_info_unix(struct inode **pinode,
374                              const unsigned char *full_path,
375                              struct super_block *sb, unsigned int xid)
376 {
377         int rc;
378         FILE_UNIX_BASIC_INFO find_data;
379         struct cifs_fattr fattr;
380         struct cifs_tcon *tcon;
381         struct tcon_link *tlink;
382         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
383
384         cifs_dbg(FYI, "Getting info on %s\n", full_path);
385
386         tlink = cifs_sb_tlink(cifs_sb);
387         if (IS_ERR(tlink))
388                 return PTR_ERR(tlink);
389         tcon = tlink_tcon(tlink);
390
391         /* could have done a find first instead but this returns more info */
392         rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
393                                   cifs_sb->local_nls, cifs_remap(cifs_sb));
394         cifs_put_tlink(tlink);
395
396         if (!rc) {
397                 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
398         } else if (rc == -EREMOTE) {
399                 cifs_create_dfs_fattr(&fattr, sb);
400                 rc = 0;
401         } else {
402                 return rc;
403         }
404
405         /* check for Minshall+French symlinks */
406         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
407                 int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
408                                              full_path);
409                 if (tmprc)
410                         cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
411         }
412
413         if (*pinode == NULL) {
414                 /* get new inode */
415                 cifs_fill_uniqueid(sb, &fattr);
416                 *pinode = cifs_iget(sb, &fattr);
417                 if (!*pinode)
418                         rc = -ENOMEM;
419         } else {
420                 /* we already have inode, update it */
421
422                 /* if uniqueid is different, return error */
423                 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
424                     CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
425                         CIFS_I(*pinode)->time = 0; /* force reval */
426                         rc = -ESTALE;
427                         goto cgiiu_exit;
428                 }
429
430                 /* if filetype is different, return error */
431                 rc = cifs_fattr_to_inode(*pinode, &fattr);
432         }
433
434 cgiiu_exit:
435         return rc;
436 }
437 #else
438 int cifs_get_inode_info_unix(struct inode **pinode,
439                              const unsigned char *full_path,
440                              struct super_block *sb, unsigned int xid)
441 {
442         return -EOPNOTSUPP;
443 }
444 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
445
446 static int
447 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
448               struct cifs_sb_info *cifs_sb, unsigned int xid)
449 {
450         int rc;
451         __u32 oplock;
452         struct tcon_link *tlink;
453         struct cifs_tcon *tcon;
454         struct cifs_fid fid;
455         struct cifs_open_parms oparms;
456         struct cifs_io_parms io_parms = {0};
457         char buf[24];
458         unsigned int bytes_read;
459         char *pbuf;
460         int buf_type = CIFS_NO_BUFFER;
461
462         pbuf = buf;
463
464         fattr->cf_mode &= ~S_IFMT;
465
466         if (fattr->cf_eof == 0) {
467                 fattr->cf_mode |= S_IFIFO;
468                 fattr->cf_dtype = DT_FIFO;
469                 return 0;
470         } else if (fattr->cf_eof < 8) {
471                 fattr->cf_mode |= S_IFREG;
472                 fattr->cf_dtype = DT_REG;
473                 return -EINVAL;  /* EOPNOTSUPP? */
474         }
475
476         tlink = cifs_sb_tlink(cifs_sb);
477         if (IS_ERR(tlink))
478                 return PTR_ERR(tlink);
479         tcon = tlink_tcon(tlink);
480
481         oparms.tcon = tcon;
482         oparms.cifs_sb = cifs_sb;
483         oparms.desired_access = GENERIC_READ;
484         oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
485         oparms.disposition = FILE_OPEN;
486         oparms.path = path;
487         oparms.fid = &fid;
488         oparms.reconnect = false;
489
490         if (tcon->ses->server->oplocks)
491                 oplock = REQ_OPLOCK;
492         else
493                 oplock = 0;
494         rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
495         if (rc) {
496                 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
497                 cifs_put_tlink(tlink);
498                 return rc;
499         }
500
501         /* Read header */
502         io_parms.netfid = fid.netfid;
503         io_parms.pid = current->tgid;
504         io_parms.tcon = tcon;
505         io_parms.offset = 0;
506         io_parms.length = 24;
507
508         rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
509                                         &bytes_read, &pbuf, &buf_type);
510         if ((rc == 0) && (bytes_read >= 8)) {
511                 if (memcmp("IntxBLK", pbuf, 8) == 0) {
512                         cifs_dbg(FYI, "Block device\n");
513                         fattr->cf_mode |= S_IFBLK;
514                         fattr->cf_dtype = DT_BLK;
515                         if (bytes_read == 24) {
516                                 /* we have enough to decode dev num */
517                                 __u64 mjr; /* major */
518                                 __u64 mnr; /* minor */
519                                 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
520                                 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
521                                 fattr->cf_rdev = MKDEV(mjr, mnr);
522                         }
523                 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
524                         cifs_dbg(FYI, "Char device\n");
525                         fattr->cf_mode |= S_IFCHR;
526                         fattr->cf_dtype = DT_CHR;
527                         if (bytes_read == 24) {
528                                 /* we have enough to decode dev num */
529                                 __u64 mjr; /* major */
530                                 __u64 mnr; /* minor */
531                                 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
532                                 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
533                                 fattr->cf_rdev = MKDEV(mjr, mnr);
534                         }
535                 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
536                         cifs_dbg(FYI, "Symlink\n");
537                         fattr->cf_mode |= S_IFLNK;
538                         fattr->cf_dtype = DT_LNK;
539                 } else {
540                         fattr->cf_mode |= S_IFREG; /* file? */
541                         fattr->cf_dtype = DT_REG;
542                         rc = -EOPNOTSUPP;
543                 }
544         } else {
545                 fattr->cf_mode |= S_IFREG; /* then it is a file */
546                 fattr->cf_dtype = DT_REG;
547                 rc = -EOPNOTSUPP; /* or some unknown SFU type */
548         }
549
550         tcon->ses->server->ops->close(xid, tcon, &fid);
551         cifs_put_tlink(tlink);
552         return rc;
553 }
554
555 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID)  /* SETFILEBITS valid bits */
556
557 /*
558  * Fetch mode bits as provided by SFU.
559  *
560  * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
561  */
562 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
563                          struct cifs_sb_info *cifs_sb, unsigned int xid)
564 {
565 #ifdef CONFIG_CIFS_XATTR
566         ssize_t rc;
567         char ea_value[4];
568         __u32 mode;
569         struct tcon_link *tlink;
570         struct cifs_tcon *tcon;
571
572         tlink = cifs_sb_tlink(cifs_sb);
573         if (IS_ERR(tlink))
574                 return PTR_ERR(tlink);
575         tcon = tlink_tcon(tlink);
576
577         if (tcon->ses->server->ops->query_all_EAs == NULL) {
578                 cifs_put_tlink(tlink);
579                 return -EOPNOTSUPP;
580         }
581
582         rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
583                         "SETFILEBITS", ea_value, 4 /* size of buf */,
584                         cifs_sb);
585         cifs_put_tlink(tlink);
586         if (rc < 0)
587                 return (int)rc;
588         else if (rc > 3) {
589                 mode = le32_to_cpu(*((__le32 *)ea_value));
590                 fattr->cf_mode &= ~SFBITS_MASK;
591                 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
592                          mode, fattr->cf_mode);
593                 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
594                 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
595         }
596
597         return 0;
598 #else
599         return -EOPNOTSUPP;
600 #endif
601 }
602
603 /* Fill a cifs_fattr struct with info from POSIX info struct */
604 static void
605 smb311_posix_info_to_fattr(struct cifs_fattr *fattr, struct smb311_posix_qinfo *info,
606                            struct super_block *sb, bool adjust_tz, bool symlink)
607 {
608         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
609         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
610
611         memset(fattr, 0, sizeof(*fattr));
612
613         /* no fattr->flags to set */
614         fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
615         fattr->cf_uniqueid = le64_to_cpu(info->Inode);
616
617         if (info->LastAccessTime)
618                 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
619         else
620                 ktime_get_coarse_real_ts64(&fattr->cf_atime);
621
622         fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
623         fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
624
625         if (adjust_tz) {
626                 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
627                 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
628         }
629
630         fattr->cf_eof = le64_to_cpu(info->EndOfFile);
631         fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
632         fattr->cf_createtime = le64_to_cpu(info->CreationTime);
633
634         fattr->cf_nlink = le32_to_cpu(info->HardLinks);
635         fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
636         /* The srv fs device id is overridden on network mount so setting rdev isn't needed here */
637         /* fattr->cf_rdev = le32_to_cpu(info->DeviceId); */
638
639         if (symlink) {
640                 fattr->cf_mode |= S_IFLNK;
641                 fattr->cf_dtype = DT_LNK;
642         } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
643                 fattr->cf_mode |= S_IFDIR;
644                 fattr->cf_dtype = DT_DIR;
645         } else { /* file */
646                 fattr->cf_mode |= S_IFREG;
647                 fattr->cf_dtype = DT_REG;
648         }
649         /* else if reparse point ... TODO: add support for FIFO and blk dev; special file types */
650
651         fattr->cf_uid = cifs_sb->ctx->linux_uid; /* TODO: map uid and gid from SID */
652         fattr->cf_gid = cifs_sb->ctx->linux_gid;
653
654         cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
655                 fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
656 }
657
658
659 /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
660 static void
661 cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
662                        struct super_block *sb, bool adjust_tz,
663                        bool symlink, u32 reparse_tag)
664 {
665         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
666         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
667
668         memset(fattr, 0, sizeof(*fattr));
669         fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
670         if (info->DeletePending)
671                 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
672
673         if (info->LastAccessTime)
674                 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
675         else
676                 ktime_get_coarse_real_ts64(&fattr->cf_atime);
677
678         fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
679         fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
680
681         if (adjust_tz) {
682                 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
683                 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
684         }
685
686         fattr->cf_eof = le64_to_cpu(info->EndOfFile);
687         fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
688         fattr->cf_createtime = le64_to_cpu(info->CreationTime);
689
690         fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
691         if (reparse_tag == IO_REPARSE_TAG_LX_SYMLINK) {
692                 fattr->cf_mode |= S_IFLNK | cifs_sb->ctx->file_mode;
693                 fattr->cf_dtype = DT_LNK;
694         } else if (reparse_tag == IO_REPARSE_TAG_LX_FIFO) {
695                 fattr->cf_mode |= S_IFIFO | cifs_sb->ctx->file_mode;
696                 fattr->cf_dtype = DT_FIFO;
697         } else if (reparse_tag == IO_REPARSE_TAG_AF_UNIX) {
698                 fattr->cf_mode |= S_IFSOCK | cifs_sb->ctx->file_mode;
699                 fattr->cf_dtype = DT_SOCK;
700         } else if (reparse_tag == IO_REPARSE_TAG_LX_CHR) {
701                 fattr->cf_mode |= S_IFCHR | cifs_sb->ctx->file_mode;
702                 fattr->cf_dtype = DT_CHR;
703         } else if (reparse_tag == IO_REPARSE_TAG_LX_BLK) {
704                 fattr->cf_mode |= S_IFBLK | cifs_sb->ctx->file_mode;
705                 fattr->cf_dtype = DT_BLK;
706         } else if (symlink) { /* TODO add more reparse tag checks */
707                 fattr->cf_mode = S_IFLNK;
708                 fattr->cf_dtype = DT_LNK;
709         } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
710                 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
711                 fattr->cf_dtype = DT_DIR;
712                 /*
713                  * Server can return wrong NumberOfLinks value for directories
714                  * when Unix extensions are disabled - fake it.
715                  */
716                 if (!tcon->unix_ext)
717                         fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
718         } else {
719                 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
720                 fattr->cf_dtype = DT_REG;
721
722                 /* clear write bits if ATTR_READONLY is set */
723                 if (fattr->cf_cifsattrs & ATTR_READONLY)
724                         fattr->cf_mode &= ~(S_IWUGO);
725
726                 /*
727                  * Don't accept zero nlink from non-unix servers unless
728                  * delete is pending.  Instead mark it as unknown.
729                  */
730                 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
731                     !info->DeletePending) {
732                         cifs_dbg(VFS, "bogus file nlink value %u\n",
733                                  fattr->cf_nlink);
734                         fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
735                 }
736         }
737
738         fattr->cf_uid = cifs_sb->ctx->linux_uid;
739         fattr->cf_gid = cifs_sb->ctx->linux_gid;
740 }
741
742 static int
743 cifs_get_file_info(struct file *filp)
744 {
745         int rc;
746         unsigned int xid;
747         FILE_ALL_INFO find_data;
748         struct cifs_fattr fattr;
749         struct inode *inode = file_inode(filp);
750         struct cifsFileInfo *cfile = filp->private_data;
751         struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
752         struct TCP_Server_Info *server = tcon->ses->server;
753
754         if (!server->ops->query_file_info)
755                 return -ENOSYS;
756
757         xid = get_xid();
758         rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
759         switch (rc) {
760         case 0:
761                 /* TODO: add support to query reparse tag */
762                 cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false,
763                                        false, 0 /* no reparse tag */);
764                 break;
765         case -EREMOTE:
766                 cifs_create_dfs_fattr(&fattr, inode->i_sb);
767                 rc = 0;
768                 break;
769         case -EOPNOTSUPP:
770         case -EINVAL:
771                 /*
772                  * FIXME: legacy server -- fall back to path-based call?
773                  * for now, just skip revalidating and mark inode for
774                  * immediate reval.
775                  */
776                 rc = 0;
777                 CIFS_I(inode)->time = 0;
778                 goto cgfi_exit;
779         default:
780                 goto cgfi_exit;
781         }
782
783         /*
784          * don't bother with SFU junk here -- just mark inode as needing
785          * revalidation.
786          */
787         fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
788         fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
789         /* if filetype is different, return error */
790         rc = cifs_fattr_to_inode(inode, &fattr);
791 cgfi_exit:
792         free_xid(xid);
793         return rc;
794 }
795
796 /* Simple function to return a 64 bit hash of string.  Rarely called */
797 static __u64 simple_hashstr(const char *str)
798 {
799         const __u64 hash_mult =  1125899906842597ULL; /* a big enough prime */
800         __u64 hash = 0;
801
802         while (*str)
803                 hash = (hash + (__u64) *str++) * hash_mult;
804
805         return hash;
806 }
807
808 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
809 /**
810  * cifs_backup_query_path_info - SMB1 fallback code to get ino
811  *
812  * Fallback code to get file metadata when we don't have access to
813  * full_path (EACCES) and have backup creds.
814  *
815  * @xid:        transaction id used to identify original request in logs
816  * @tcon:       information about the server share we have mounted
817  * @sb: the superblock stores info such as disk space available
818  * @full_path:  name of the file we are getting the metadata for
819  * @resp_buf:   will be set to cifs resp buf and needs to be freed with
820  *              cifs_buf_release() when done with @data
821  * @data:       will be set to search info result buffer
822  */
823 static int
824 cifs_backup_query_path_info(int xid,
825                             struct cifs_tcon *tcon,
826                             struct super_block *sb,
827                             const char *full_path,
828                             void **resp_buf,
829                             FILE_ALL_INFO **data)
830 {
831         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
832         struct cifs_search_info info = {0};
833         u16 flags;
834         int rc;
835
836         *resp_buf = NULL;
837         info.endOfSearch = false;
838         if (tcon->unix_ext)
839                 info.info_level = SMB_FIND_FILE_UNIX;
840         else if ((tcon->ses->capabilities &
841                   tcon->ses->server->vals->cap_nt_find) == 0)
842                 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
843         else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
844                 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
845         else /* no srvino useful for fallback to some netapp */
846                 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
847
848         flags = CIFS_SEARCH_CLOSE_ALWAYS |
849                 CIFS_SEARCH_CLOSE_AT_END |
850                 CIFS_SEARCH_BACKUP_SEARCH;
851
852         rc = CIFSFindFirst(xid, tcon, full_path,
853                            cifs_sb, NULL, flags, &info, false);
854         if (rc)
855                 return rc;
856
857         *resp_buf = (void *)info.ntwrk_buf_start;
858         *data = (FILE_ALL_INFO *)info.srch_entries_start;
859         return 0;
860 }
861 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
862
863 static void
864 cifs_set_fattr_ino(int xid,
865                    struct cifs_tcon *tcon,
866                    struct super_block *sb,
867                    struct inode **inode,
868                    const char *full_path,
869                    FILE_ALL_INFO *data,
870                    struct cifs_fattr *fattr)
871 {
872         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
873         struct TCP_Server_Info *server = tcon->ses->server;
874         int rc;
875
876         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
877                 if (*inode)
878                         fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
879                 else
880                         fattr->cf_uniqueid = iunique(sb, ROOT_I);
881                 return;
882         }
883
884         /*
885          * If we have an inode pass a NULL tcon to ensure we don't
886          * make a round trip to the server. This only works for SMB2+.
887          */
888         rc = server->ops->get_srv_inum(xid,
889                                        *inode ? NULL : tcon,
890                                        cifs_sb, full_path,
891                                        &fattr->cf_uniqueid,
892                                        data);
893         if (rc) {
894                 /*
895                  * If that fails reuse existing ino or generate one
896                  * and disable server ones
897                  */
898                 if (*inode)
899                         fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
900                 else {
901                         fattr->cf_uniqueid = iunique(sb, ROOT_I);
902                         cifs_autodisable_serverino(cifs_sb);
903                 }
904                 return;
905         }
906
907         /* If no errors, check for zero root inode (invalid) */
908         if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
909                 cifs_dbg(FYI, "Invalid (0) inodenum\n");
910                 if (*inode) {
911                         /* reuse */
912                         fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
913                 } else {
914                         /* make an ino by hashing the UNC */
915                         fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
916                         fattr->cf_uniqueid = simple_hashstr(tcon->treeName);
917                 }
918         }
919 }
920
921 static inline bool is_inode_cache_good(struct inode *ino)
922 {
923         return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
924 }
925
926 int
927 cifs_get_inode_info(struct inode **inode,
928                     const char *full_path,
929                     FILE_ALL_INFO *in_data,
930                     struct super_block *sb, int xid,
931                     const struct cifs_fid *fid)
932 {
933
934         struct cifs_tcon *tcon;
935         struct TCP_Server_Info *server;
936         struct tcon_link *tlink;
937         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
938         bool adjust_tz = false;
939         struct cifs_fattr fattr = {0};
940         bool is_reparse_point = false;
941         FILE_ALL_INFO *data = in_data;
942         FILE_ALL_INFO *tmp_data = NULL;
943         void *smb1_backup_rsp_buf = NULL;
944         int rc = 0;
945         int tmprc = 0;
946         __u32 reparse_tag = 0;
947
948         tlink = cifs_sb_tlink(cifs_sb);
949         if (IS_ERR(tlink))
950                 return PTR_ERR(tlink);
951         tcon = tlink_tcon(tlink);
952         server = tcon->ses->server;
953
954         /*
955          * 1. Fetch file metadata if not provided (data)
956          */
957
958         if (!data) {
959                 if (is_inode_cache_good(*inode)) {
960                         cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
961                         goto out;
962                 }
963                 tmp_data = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
964                 if (!tmp_data) {
965                         rc = -ENOMEM;
966                         goto out;
967                 }
968                 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
969                                                  full_path, tmp_data,
970                                                  &adjust_tz, &is_reparse_point);
971 #ifdef CONFIG_CIFS_DFS_UPCALL
972                 if (rc == -ENOENT && is_tcon_dfs(tcon))
973                         rc = cifs_dfs_query_info_nonascii_quirk(xid, tcon,
974                                                                 cifs_sb,
975                                                                 full_path);
976 #endif
977                 data = tmp_data;
978         }
979
980         /*
981          * 2. Convert it to internal cifs metadata (fattr)
982          */
983
984         switch (rc) {
985         case 0:
986                 /*
987                  * If the file is a reparse point, it is more complicated
988                  * since we have to check if its reparse tag matches a known
989                  * special file type e.g. symlink or fifo or char etc.
990                  */
991                 if ((le32_to_cpu(data->Attributes) & ATTR_REPARSE) &&
992                     server->ops->query_reparse_tag) {
993                         rc = server->ops->query_reparse_tag(xid, tcon, cifs_sb,
994                                                 full_path, &reparse_tag);
995                         cifs_dbg(FYI, "reparse tag 0x%x\n", reparse_tag);
996                 }
997                 cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz,
998                                        is_reparse_point, reparse_tag);
999                 break;
1000         case -EREMOTE:
1001                 /* DFS link, no metadata available on this server */
1002                 cifs_create_dfs_fattr(&fattr, sb);
1003                 rc = 0;
1004                 break;
1005         case -EACCES:
1006 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1007                 /*
1008                  * perm errors, try again with backup flags if possible
1009                  *
1010                  * For SMB2 and later the backup intent flag
1011                  * is already sent if needed on open and there
1012                  * is no path based FindFirst operation to use
1013                  * to retry with
1014                  */
1015                 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1016                         /* for easier reading */
1017                         FILE_DIRECTORY_INFO *fdi;
1018                         SEARCH_ID_FULL_DIR_INFO *si;
1019
1020                         rc = cifs_backup_query_path_info(xid, tcon, sb,
1021                                                          full_path,
1022                                                          &smb1_backup_rsp_buf,
1023                                                          &data);
1024                         if (rc)
1025                                 goto out;
1026
1027                         fdi = (FILE_DIRECTORY_INFO *)data;
1028                         si = (SEARCH_ID_FULL_DIR_INFO *)data;
1029
1030                         cifs_dir_info_to_fattr(&fattr, fdi, cifs_sb);
1031                         fattr.cf_uniqueid = le64_to_cpu(si->UniqueId);
1032                         /* uniqueid set, skip get inum step */
1033                         goto handle_mnt_opt;
1034                 } else {
1035                         /* nothing we can do, bail out */
1036                         goto out;
1037                 }
1038 #else
1039                 goto out;
1040 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1041                 break;
1042         default:
1043                 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1044                 goto out;
1045         }
1046
1047         /*
1048          * 3. Get or update inode number (fattr.cf_uniqueid)
1049          */
1050
1051         cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, &fattr);
1052
1053         /*
1054          * 4. Tweak fattr based on mount options
1055          */
1056 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1057 handle_mnt_opt:
1058 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1059         /* query for SFU type info if supported and needed */
1060         if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
1061             cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
1062                 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
1063                 if (tmprc)
1064                         cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1065         }
1066
1067         /* fill in 0777 bits from ACL */
1068         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1069                 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, true,
1070                                        full_path, fid);
1071                 if (rc == -EREMOTE)
1072                         rc = 0;
1073                 if (rc) {
1074                         cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1075                                  __func__, rc);
1076                         goto out;
1077                 }
1078         } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1079                 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, false,
1080                                        full_path, fid);
1081                 if (rc == -EREMOTE)
1082                         rc = 0;
1083                 if (rc) {
1084                         cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1085                                  __func__, rc);
1086                         goto out;
1087                 }
1088         }
1089
1090         /* fill in remaining high mode bits e.g. SUID, VTX */
1091         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1092                 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
1093
1094         /* check for Minshall+French symlinks */
1095         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1096                 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1097                                          full_path);
1098                 if (tmprc)
1099                         cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1100         }
1101
1102         /*
1103          * 5. Update inode with final fattr data
1104          */
1105
1106         if (!*inode) {
1107                 *inode = cifs_iget(sb, &fattr);
1108                 if (!*inode)
1109                         rc = -ENOMEM;
1110         } else {
1111                 /* we already have inode, update it */
1112
1113                 /* if uniqueid is different, return error */
1114                 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1115                     CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1116                         CIFS_I(*inode)->time = 0; /* force reval */
1117                         rc = -ESTALE;
1118                         goto out;
1119                 }
1120                 /* if filetype is different, return error */
1121                 rc = cifs_fattr_to_inode(*inode, &fattr);
1122         }
1123 out:
1124         cifs_buf_release(smb1_backup_rsp_buf);
1125         cifs_put_tlink(tlink);
1126         kfree(tmp_data);
1127         return rc;
1128 }
1129
1130 int
1131 smb311_posix_get_inode_info(struct inode **inode,
1132                     const char *full_path,
1133                     struct super_block *sb, unsigned int xid)
1134 {
1135         struct cifs_tcon *tcon;
1136         struct tcon_link *tlink;
1137         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1138         bool adjust_tz = false;
1139         struct cifs_fattr fattr = {0};
1140         bool symlink = false;
1141         struct smb311_posix_qinfo *data = NULL;
1142         int rc = 0;
1143         int tmprc = 0;
1144
1145         tlink = cifs_sb_tlink(cifs_sb);
1146         if (IS_ERR(tlink))
1147                 return PTR_ERR(tlink);
1148         tcon = tlink_tcon(tlink);
1149
1150         /*
1151          * 1. Fetch file metadata
1152          */
1153
1154         if (is_inode_cache_good(*inode)) {
1155                 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1156                 goto out;
1157         }
1158         data = kmalloc(sizeof(struct smb311_posix_qinfo), GFP_KERNEL);
1159         if (!data) {
1160                 rc = -ENOMEM;
1161                 goto out;
1162         }
1163
1164         rc = smb311_posix_query_path_info(xid, tcon, cifs_sb,
1165                                                   full_path, data,
1166                                                   &adjust_tz, &symlink);
1167
1168         /*
1169          * 2. Convert it to internal cifs metadata (fattr)
1170          */
1171
1172         switch (rc) {
1173         case 0:
1174                 smb311_posix_info_to_fattr(&fattr, data, sb, adjust_tz, symlink);
1175                 break;
1176         case -EREMOTE:
1177                 /* DFS link, no metadata available on this server */
1178                 cifs_create_dfs_fattr(&fattr, sb);
1179                 rc = 0;
1180                 break;
1181         case -EACCES:
1182                 /*
1183                  * For SMB2 and later the backup intent flag
1184                  * is already sent if needed on open and there
1185                  * is no path based FindFirst operation to use
1186                  * to retry with so nothing we can do, bail out
1187                  */
1188                 goto out;
1189         default:
1190                 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1191                 goto out;
1192         }
1193
1194
1195         /*
1196          * 3. Tweak fattr based on mount options
1197          */
1198
1199         /* check for Minshall+French symlinks */
1200         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1201                 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
1202                                          full_path);
1203                 if (tmprc)
1204                         cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1205         }
1206
1207         /*
1208          * 4. Update inode with final fattr data
1209          */
1210
1211         if (!*inode) {
1212                 *inode = cifs_iget(sb, &fattr);
1213                 if (!*inode)
1214                         rc = -ENOMEM;
1215         } else {
1216                 /* we already have inode, update it */
1217
1218                 /* if uniqueid is different, return error */
1219                 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
1220                     CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
1221                         CIFS_I(*inode)->time = 0; /* force reval */
1222                         rc = -ESTALE;
1223                         goto out;
1224                 }
1225
1226                 /* if filetype is different, return error */
1227                 rc = cifs_fattr_to_inode(*inode, &fattr);
1228         }
1229 out:
1230         cifs_put_tlink(tlink);
1231         kfree(data);
1232         return rc;
1233 }
1234
1235
1236 static const struct inode_operations cifs_ipc_inode_ops = {
1237         .lookup = cifs_lookup,
1238 };
1239
1240 static int
1241 cifs_find_inode(struct inode *inode, void *opaque)
1242 {
1243         struct cifs_fattr *fattr = opaque;
1244
1245         /* don't match inode with different uniqueid */
1246         if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1247                 return 0;
1248
1249         /* use createtime like an i_generation field */
1250         if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1251                 return 0;
1252
1253         /* don't match inode of different type */
1254         if (inode_wrong_type(inode, fattr->cf_mode))
1255                 return 0;
1256
1257         /* if it's not a directory or has no dentries, then flag it */
1258         if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1259                 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1260
1261         return 1;
1262 }
1263
1264 static int
1265 cifs_init_inode(struct inode *inode, void *opaque)
1266 {
1267         struct cifs_fattr *fattr = opaque;
1268
1269         CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1270         CIFS_I(inode)->createtime = fattr->cf_createtime;
1271         return 0;
1272 }
1273
1274 /*
1275  * walk dentry list for an inode and report whether it has aliases that
1276  * are hashed. We use this to determine if a directory inode can actually
1277  * be used.
1278  */
1279 static bool
1280 inode_has_hashed_dentries(struct inode *inode)
1281 {
1282         struct dentry *dentry;
1283
1284         spin_lock(&inode->i_lock);
1285         hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1286                 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1287                         spin_unlock(&inode->i_lock);
1288                         return true;
1289                 }
1290         }
1291         spin_unlock(&inode->i_lock);
1292         return false;
1293 }
1294
1295 /* Given fattrs, get a corresponding inode */
1296 struct inode *
1297 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1298 {
1299         unsigned long hash;
1300         struct inode *inode;
1301
1302 retry_iget5_locked:
1303         cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1304
1305         /* hash down to 32-bits on 32-bit arch */
1306         hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1307
1308         inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1309         if (inode) {
1310                 /* was there a potentially problematic inode collision? */
1311                 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1312                         fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1313
1314                         if (inode_has_hashed_dentries(inode)) {
1315                                 cifs_autodisable_serverino(CIFS_SB(sb));
1316                                 iput(inode);
1317                                 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1318                                 goto retry_iget5_locked;
1319                         }
1320                 }
1321
1322                 /* can't fail - see cifs_find_inode() */
1323                 cifs_fattr_to_inode(inode, fattr);
1324                 if (sb->s_flags & SB_NOATIME)
1325                         inode->i_flags |= S_NOATIME | S_NOCMTIME;
1326                 if (inode->i_state & I_NEW) {
1327                         inode->i_ino = hash;
1328                         cifs_fscache_get_inode_cookie(inode);
1329                         unlock_new_inode(inode);
1330                 }
1331         }
1332
1333         return inode;
1334 }
1335
1336 /* gets root inode */
1337 struct inode *cifs_root_iget(struct super_block *sb)
1338 {
1339         unsigned int xid;
1340         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1341         struct inode *inode = NULL;
1342         long rc;
1343         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1344         char *path = NULL;
1345         int len;
1346
1347         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1348             && cifs_sb->prepath) {
1349                 len = strlen(cifs_sb->prepath);
1350                 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1351                 if (path == NULL)
1352                         return ERR_PTR(-ENOMEM);
1353                 path[0] = '/';
1354                 memcpy(path+1, cifs_sb->prepath, len);
1355         } else {
1356                 path = kstrdup("", GFP_KERNEL);
1357                 if (path == NULL)
1358                         return ERR_PTR(-ENOMEM);
1359         }
1360
1361         xid = get_xid();
1362         if (tcon->unix_ext) {
1363                 rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
1364                 /* some servers mistakenly claim POSIX support */
1365                 if (rc != -EOPNOTSUPP)
1366                         goto iget_no_retry;
1367                 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1368                 tcon->unix_ext = false;
1369         }
1370
1371         convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1372         if (tcon->posix_extensions)
1373                 rc = smb311_posix_get_inode_info(&inode, path, sb, xid);
1374         else
1375                 rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
1376
1377 iget_no_retry:
1378         if (!inode) {
1379                 inode = ERR_PTR(rc);
1380                 goto out;
1381         }
1382
1383         if (rc && tcon->pipe) {
1384                 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1385                 spin_lock(&inode->i_lock);
1386                 inode->i_mode |= S_IFDIR;
1387                 set_nlink(inode, 2);
1388                 inode->i_op = &cifs_ipc_inode_ops;
1389                 inode->i_fop = &simple_dir_operations;
1390                 inode->i_uid = cifs_sb->ctx->linux_uid;
1391                 inode->i_gid = cifs_sb->ctx->linux_gid;
1392                 spin_unlock(&inode->i_lock);
1393         } else if (rc) {
1394                 iget_failed(inode);
1395                 inode = ERR_PTR(rc);
1396         }
1397
1398 out:
1399         kfree(path);
1400         free_xid(xid);
1401         return inode;
1402 }
1403
1404 int
1405 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1406                    const char *full_path, __u32 dosattr)
1407 {
1408         bool set_time = false;
1409         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1410         struct TCP_Server_Info *server;
1411         FILE_BASIC_INFO info_buf;
1412
1413         if (attrs == NULL)
1414                 return -EINVAL;
1415
1416         server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1417         if (!server->ops->set_file_info)
1418                 return -ENOSYS;
1419
1420         info_buf.Pad = 0;
1421
1422         if (attrs->ia_valid & ATTR_ATIME) {
1423                 set_time = true;
1424                 info_buf.LastAccessTime =
1425                         cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1426         } else
1427                 info_buf.LastAccessTime = 0;
1428
1429         if (attrs->ia_valid & ATTR_MTIME) {
1430                 set_time = true;
1431                 info_buf.LastWriteTime =
1432                     cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1433         } else
1434                 info_buf.LastWriteTime = 0;
1435
1436         /*
1437          * Samba throws this field away, but windows may actually use it.
1438          * Do not set ctime unless other time stamps are changed explicitly
1439          * (i.e. by utimes()) since we would then have a mix of client and
1440          * server times.
1441          */
1442         if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1443                 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1444                 info_buf.ChangeTime =
1445                     cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1446         } else
1447                 info_buf.ChangeTime = 0;
1448
1449         info_buf.CreationTime = 0;      /* don't change */
1450         info_buf.Attributes = cpu_to_le32(dosattr);
1451
1452         return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1453 }
1454
1455 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1456 /*
1457  * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1458  * and rename it to a random name that hopefully won't conflict with
1459  * anything else.
1460  */
1461 int
1462 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1463                            const unsigned int xid)
1464 {
1465         int oplock = 0;
1466         int rc;
1467         struct cifs_fid fid;
1468         struct cifs_open_parms oparms;
1469         struct inode *inode = d_inode(dentry);
1470         struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1471         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1472         struct tcon_link *tlink;
1473         struct cifs_tcon *tcon;
1474         __u32 dosattr, origattr;
1475         FILE_BASIC_INFO *info_buf = NULL;
1476
1477         tlink = cifs_sb_tlink(cifs_sb);
1478         if (IS_ERR(tlink))
1479                 return PTR_ERR(tlink);
1480         tcon = tlink_tcon(tlink);
1481
1482         /*
1483          * We cannot rename the file if the server doesn't support
1484          * CAP_INFOLEVEL_PASSTHRU
1485          */
1486         if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1487                 rc = -EBUSY;
1488                 goto out;
1489         }
1490
1491         oparms.tcon = tcon;
1492         oparms.cifs_sb = cifs_sb;
1493         oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1494         oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
1495         oparms.disposition = FILE_OPEN;
1496         oparms.path = full_path;
1497         oparms.fid = &fid;
1498         oparms.reconnect = false;
1499
1500         rc = CIFS_open(xid, &oparms, &oplock, NULL);
1501         if (rc != 0)
1502                 goto out;
1503
1504         origattr = cifsInode->cifsAttrs;
1505         if (origattr == 0)
1506                 origattr |= ATTR_NORMAL;
1507
1508         dosattr = origattr & ~ATTR_READONLY;
1509         if (dosattr == 0)
1510                 dosattr |= ATTR_NORMAL;
1511         dosattr |= ATTR_HIDDEN;
1512
1513         /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1514         if (dosattr != origattr) {
1515                 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1516                 if (info_buf == NULL) {
1517                         rc = -ENOMEM;
1518                         goto out_close;
1519                 }
1520                 info_buf->Attributes = cpu_to_le32(dosattr);
1521                 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1522                                         current->tgid);
1523                 /* although we would like to mark the file hidden
1524                    if that fails we will still try to rename it */
1525                 if (!rc)
1526                         cifsInode->cifsAttrs = dosattr;
1527                 else
1528                         dosattr = origattr; /* since not able to change them */
1529         }
1530
1531         /* rename the file */
1532         rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1533                                    cifs_sb->local_nls,
1534                                    cifs_remap(cifs_sb));
1535         if (rc != 0) {
1536                 rc = -EBUSY;
1537                 goto undo_setattr;
1538         }
1539
1540         /* try to set DELETE_ON_CLOSE */
1541         if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1542                 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1543                                                current->tgid);
1544                 /*
1545                  * some samba versions return -ENOENT when we try to set the
1546                  * file disposition here. Likely a samba bug, but work around
1547                  * it for now. This means that some cifsXXX files may hang
1548                  * around after they shouldn't.
1549                  *
1550                  * BB: remove this hack after more servers have the fix
1551                  */
1552                 if (rc == -ENOENT)
1553                         rc = 0;
1554                 else if (rc != 0) {
1555                         rc = -EBUSY;
1556                         goto undo_rename;
1557                 }
1558                 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1559         }
1560
1561 out_close:
1562         CIFSSMBClose(xid, tcon, fid.netfid);
1563 out:
1564         kfree(info_buf);
1565         cifs_put_tlink(tlink);
1566         return rc;
1567
1568         /*
1569          * reset everything back to the original state. Don't bother
1570          * dealing with errors here since we can't do anything about
1571          * them anyway.
1572          */
1573 undo_rename:
1574         CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1575                                 cifs_sb->local_nls, cifs_remap(cifs_sb));
1576 undo_setattr:
1577         if (dosattr != origattr) {
1578                 info_buf->Attributes = cpu_to_le32(origattr);
1579                 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1580                                         current->tgid))
1581                         cifsInode->cifsAttrs = origattr;
1582         }
1583
1584         goto out_close;
1585 }
1586 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1587
1588 /* copied from fs/nfs/dir.c with small changes */
1589 static void
1590 cifs_drop_nlink(struct inode *inode)
1591 {
1592         spin_lock(&inode->i_lock);
1593         if (inode->i_nlink > 0)
1594                 drop_nlink(inode);
1595         spin_unlock(&inode->i_lock);
1596 }
1597
1598 /*
1599  * If d_inode(dentry) is null (usually meaning the cached dentry
1600  * is a negative dentry) then we would attempt a standard SMB delete, but
1601  * if that fails we can not attempt the fall back mechanisms on EACCES
1602  * but will return the EACCES to the caller. Note that the VFS does not call
1603  * unlink on negative dentries currently.
1604  */
1605 int cifs_unlink(struct inode *dir, struct dentry *dentry)
1606 {
1607         int rc = 0;
1608         unsigned int xid;
1609         const char *full_path;
1610         void *page;
1611         struct inode *inode = d_inode(dentry);
1612         struct cifsInodeInfo *cifs_inode;
1613         struct super_block *sb = dir->i_sb;
1614         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1615         struct tcon_link *tlink;
1616         struct cifs_tcon *tcon;
1617         struct TCP_Server_Info *server;
1618         struct iattr *attrs = NULL;
1619         __u32 dosattr = 0, origattr = 0;
1620
1621         cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1622
1623         if (unlikely(cifs_forced_shutdown(cifs_sb)))
1624                 return -EIO;
1625
1626         tlink = cifs_sb_tlink(cifs_sb);
1627         if (IS_ERR(tlink))
1628                 return PTR_ERR(tlink);
1629         tcon = tlink_tcon(tlink);
1630         server = tcon->ses->server;
1631
1632         xid = get_xid();
1633         page = alloc_dentry_path();
1634
1635         if (tcon->nodelete) {
1636                 rc = -EACCES;
1637                 goto unlink_out;
1638         }
1639
1640         /* Unlink can be called from rename so we can not take the
1641          * sb->s_vfs_rename_mutex here */
1642         full_path = build_path_from_dentry(dentry, page);
1643         if (IS_ERR(full_path)) {
1644                 rc = PTR_ERR(full_path);
1645                 goto unlink_out;
1646         }
1647
1648         cifs_close_deferred_file_under_dentry(tcon, full_path);
1649 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1650         if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1651                                 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1652                 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1653                         SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1654                         cifs_remap(cifs_sb));
1655                 cifs_dbg(FYI, "posix del rc %d\n", rc);
1656                 if ((rc == 0) || (rc == -ENOENT))
1657                         goto psx_del_no_retry;
1658         }
1659 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1660
1661 retry_std_delete:
1662         if (!server->ops->unlink) {
1663                 rc = -ENOSYS;
1664                 goto psx_del_no_retry;
1665         }
1666
1667         rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
1668
1669 psx_del_no_retry:
1670         if (!rc) {
1671                 if (inode)
1672                         cifs_drop_nlink(inode);
1673         } else if (rc == -ENOENT) {
1674                 d_drop(dentry);
1675         } else if (rc == -EBUSY) {
1676                 if (server->ops->rename_pending_delete) {
1677                         rc = server->ops->rename_pending_delete(full_path,
1678                                                                 dentry, xid);
1679                         if (rc == 0)
1680                                 cifs_drop_nlink(inode);
1681                 }
1682         } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
1683                 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1684                 if (attrs == NULL) {
1685                         rc = -ENOMEM;
1686                         goto out_reval;
1687                 }
1688
1689                 /* try to reset dos attributes */
1690                 cifs_inode = CIFS_I(inode);
1691                 origattr = cifs_inode->cifsAttrs;
1692                 if (origattr == 0)
1693                         origattr |= ATTR_NORMAL;
1694                 dosattr = origattr & ~ATTR_READONLY;
1695                 if (dosattr == 0)
1696                         dosattr |= ATTR_NORMAL;
1697                 dosattr |= ATTR_HIDDEN;
1698
1699                 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1700                 if (rc != 0)
1701                         goto out_reval;
1702
1703                 goto retry_std_delete;
1704         }
1705
1706         /* undo the setattr if we errored out and it's needed */
1707         if (rc != 0 && dosattr != 0)
1708                 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1709
1710 out_reval:
1711         if (inode) {
1712                 cifs_inode = CIFS_I(inode);
1713                 cifs_inode->time = 0;   /* will force revalidate to get info
1714                                            when needed */
1715                 inode->i_ctime = current_time(inode);
1716         }
1717         dir->i_ctime = dir->i_mtime = current_time(dir);
1718         cifs_inode = CIFS_I(dir);
1719         CIFS_I(dir)->time = 0;  /* force revalidate of dir as well */
1720 unlink_out:
1721         free_dentry_path(page);
1722         kfree(attrs);
1723         free_xid(xid);
1724         cifs_put_tlink(tlink);
1725         return rc;
1726 }
1727
1728 static int
1729 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
1730                  const char *full_path, struct cifs_sb_info *cifs_sb,
1731                  struct cifs_tcon *tcon, const unsigned int xid)
1732 {
1733         int rc = 0;
1734         struct inode *inode = NULL;
1735
1736         if (tcon->posix_extensions)
1737                 rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
1738 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1739         else if (tcon->unix_ext)
1740                 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
1741                                               xid);
1742 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1743         else
1744                 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1745                                          xid, NULL);
1746
1747         if (rc)
1748                 return rc;
1749
1750         if (!S_ISDIR(inode->i_mode)) {
1751                 /*
1752                  * mkdir succeeded, but another client has managed to remove the
1753                  * sucker and replace it with non-directory.  Return success,
1754                  * but don't leave the child in dcache.
1755                  */
1756                  iput(inode);
1757                  d_drop(dentry);
1758                  return 0;
1759         }
1760         /*
1761          * setting nlink not necessary except in cases where we failed to get it
1762          * from the server or was set bogus. Also, since this is a brand new
1763          * inode, no need to grab the i_lock before setting the i_nlink.
1764          */
1765         if (inode->i_nlink < 2)
1766                 set_nlink(inode, 2);
1767         mode &= ~current_umask();
1768         /* must turn on setgid bit if parent dir has it */
1769         if (parent->i_mode & S_ISGID)
1770                 mode |= S_ISGID;
1771
1772 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1773         if (tcon->unix_ext) {
1774                 struct cifs_unix_set_info_args args = {
1775                         .mode   = mode,
1776                         .ctime  = NO_CHANGE_64,
1777                         .atime  = NO_CHANGE_64,
1778                         .mtime  = NO_CHANGE_64,
1779                         .device = 0,
1780                 };
1781                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1782                         args.uid = current_fsuid();
1783                         if (parent->i_mode & S_ISGID)
1784                                 args.gid = parent->i_gid;
1785                         else
1786                                 args.gid = current_fsgid();
1787                 } else {
1788                         args.uid = INVALID_UID; /* no change */
1789                         args.gid = INVALID_GID; /* no change */
1790                 }
1791                 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1792                                        cifs_sb->local_nls,
1793                                        cifs_remap(cifs_sb));
1794         } else {
1795 #else
1796         {
1797 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1798                 struct TCP_Server_Info *server = tcon->ses->server;
1799                 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1800                     (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
1801                         server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
1802                                                    tcon, xid);
1803                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1804                         inode->i_mode = (mode | S_IFDIR);
1805
1806                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1807                         inode->i_uid = current_fsuid();
1808                         if (inode->i_mode & S_ISGID)
1809                                 inode->i_gid = parent->i_gid;
1810                         else
1811                                 inode->i_gid = current_fsgid();
1812                 }
1813         }
1814         d_instantiate(dentry, inode);
1815         return 0;
1816 }
1817
1818 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1819 static int
1820 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1821                  const char *full_path, struct cifs_sb_info *cifs_sb,
1822                  struct cifs_tcon *tcon, const unsigned int xid)
1823 {
1824         int rc = 0;
1825         u32 oplock = 0;
1826         FILE_UNIX_BASIC_INFO *info = NULL;
1827         struct inode *newinode = NULL;
1828         struct cifs_fattr fattr;
1829
1830         info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1831         if (info == NULL) {
1832                 rc = -ENOMEM;
1833                 goto posix_mkdir_out;
1834         }
1835
1836         mode &= ~current_umask();
1837         rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1838                              NULL /* netfid */, info, &oplock, full_path,
1839                              cifs_sb->local_nls, cifs_remap(cifs_sb));
1840         if (rc == -EOPNOTSUPP)
1841                 goto posix_mkdir_out;
1842         else if (rc) {
1843                 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
1844                 d_drop(dentry);
1845                 goto posix_mkdir_out;
1846         }
1847
1848         if (info->Type == cpu_to_le32(-1))
1849                 /* no return info, go query for it */
1850                 goto posix_mkdir_get_info;
1851         /*
1852          * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1853          * need to set uid/gid.
1854          */
1855
1856         cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1857         cifs_fill_uniqueid(inode->i_sb, &fattr);
1858         newinode = cifs_iget(inode->i_sb, &fattr);
1859         if (!newinode)
1860                 goto posix_mkdir_get_info;
1861
1862         d_instantiate(dentry, newinode);
1863
1864 #ifdef CONFIG_CIFS_DEBUG2
1865         cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1866                  dentry, dentry, newinode);
1867
1868         if (newinode->i_nlink != 2)
1869                 cifs_dbg(FYI, "unexpected number of links %d\n",
1870                          newinode->i_nlink);
1871 #endif
1872
1873 posix_mkdir_out:
1874         kfree(info);
1875         return rc;
1876 posix_mkdir_get_info:
1877         rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1878                               xid);
1879         goto posix_mkdir_out;
1880 }
1881 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1882
1883 int cifs_mkdir(struct user_namespace *mnt_userns, struct inode *inode,
1884                struct dentry *direntry, umode_t mode)
1885 {
1886         int rc = 0;
1887         unsigned int xid;
1888         struct cifs_sb_info *cifs_sb;
1889         struct tcon_link *tlink;
1890         struct cifs_tcon *tcon;
1891         struct TCP_Server_Info *server;
1892         const char *full_path;
1893         void *page;
1894
1895         cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
1896                  mode, inode);
1897
1898         cifs_sb = CIFS_SB(inode->i_sb);
1899         if (unlikely(cifs_forced_shutdown(cifs_sb)))
1900                 return -EIO;
1901         tlink = cifs_sb_tlink(cifs_sb);
1902         if (IS_ERR(tlink))
1903                 return PTR_ERR(tlink);
1904         tcon = tlink_tcon(tlink);
1905
1906         xid = get_xid();
1907
1908         page = alloc_dentry_path();
1909         full_path = build_path_from_dentry(direntry, page);
1910         if (IS_ERR(full_path)) {
1911                 rc = PTR_ERR(full_path);
1912                 goto mkdir_out;
1913         }
1914
1915         server = tcon->ses->server;
1916
1917         if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
1918                 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
1919                                               cifs_sb);
1920                 d_drop(direntry); /* for time being always refresh inode info */
1921                 goto mkdir_out;
1922         }
1923
1924 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1925         if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1926                                 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1927                 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1928                                       tcon, xid);
1929                 if (rc != -EOPNOTSUPP)
1930                         goto mkdir_out;
1931         }
1932 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1933
1934         if (!server->ops->mkdir) {
1935                 rc = -ENOSYS;
1936                 goto mkdir_out;
1937         }
1938
1939         /* BB add setting the equivalent of mode via CreateX w/ACLs */
1940         rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
1941         if (rc) {
1942                 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1943                 d_drop(direntry);
1944                 goto mkdir_out;
1945         }
1946
1947         /* TODO: skip this for smb2/smb3 */
1948         rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1949                               xid);
1950 mkdir_out:
1951         /*
1952          * Force revalidate to get parent dir info when needed since cached
1953          * attributes are invalid now.
1954          */
1955         CIFS_I(inode)->time = 0;
1956         free_dentry_path(page);
1957         free_xid(xid);
1958         cifs_put_tlink(tlink);
1959         return rc;
1960 }
1961
1962 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1963 {
1964         int rc = 0;
1965         unsigned int xid;
1966         struct cifs_sb_info *cifs_sb;
1967         struct tcon_link *tlink;
1968         struct cifs_tcon *tcon;
1969         struct TCP_Server_Info *server;
1970         const char *full_path;
1971         void *page = alloc_dentry_path();
1972         struct cifsInodeInfo *cifsInode;
1973
1974         cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
1975
1976         xid = get_xid();
1977
1978         full_path = build_path_from_dentry(direntry, page);
1979         if (IS_ERR(full_path)) {
1980                 rc = PTR_ERR(full_path);
1981                 goto rmdir_exit;
1982         }
1983
1984         cifs_sb = CIFS_SB(inode->i_sb);
1985         if (unlikely(cifs_forced_shutdown(cifs_sb))) {
1986                 rc = -EIO;
1987                 goto rmdir_exit;
1988         }
1989
1990         tlink = cifs_sb_tlink(cifs_sb);
1991         if (IS_ERR(tlink)) {
1992                 rc = PTR_ERR(tlink);
1993                 goto rmdir_exit;
1994         }
1995         tcon = tlink_tcon(tlink);
1996         server = tcon->ses->server;
1997
1998         if (!server->ops->rmdir) {
1999                 rc = -ENOSYS;
2000                 cifs_put_tlink(tlink);
2001                 goto rmdir_exit;
2002         }
2003
2004         if (tcon->nodelete) {
2005                 rc = -EACCES;
2006                 cifs_put_tlink(tlink);
2007                 goto rmdir_exit;
2008         }
2009
2010         rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2011         cifs_put_tlink(tlink);
2012
2013         if (!rc) {
2014                 spin_lock(&d_inode(direntry)->i_lock);
2015                 i_size_write(d_inode(direntry), 0);
2016                 clear_nlink(d_inode(direntry));
2017                 spin_unlock(&d_inode(direntry)->i_lock);
2018         }
2019
2020         cifsInode = CIFS_I(d_inode(direntry));
2021         /* force revalidate to go get info when needed */
2022         cifsInode->time = 0;
2023
2024         cifsInode = CIFS_I(inode);
2025         /*
2026          * Force revalidate to get parent dir info when needed since cached
2027          * attributes are invalid now.
2028          */
2029         cifsInode->time = 0;
2030
2031         d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
2032                 current_time(inode);
2033
2034 rmdir_exit:
2035         free_dentry_path(page);
2036         free_xid(xid);
2037         return rc;
2038 }
2039
2040 static int
2041 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2042                const char *from_path, struct dentry *to_dentry,
2043                const char *to_path)
2044 {
2045         struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2046         struct tcon_link *tlink;
2047         struct cifs_tcon *tcon;
2048         struct TCP_Server_Info *server;
2049 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2050         struct cifs_fid fid;
2051         struct cifs_open_parms oparms;
2052         int oplock;
2053 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2054         int rc;
2055
2056         tlink = cifs_sb_tlink(cifs_sb);
2057         if (IS_ERR(tlink))
2058                 return PTR_ERR(tlink);
2059         tcon = tlink_tcon(tlink);
2060         server = tcon->ses->server;
2061
2062         if (!server->ops->rename)
2063                 return -ENOSYS;
2064
2065         /* try path-based rename first */
2066         rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
2067
2068         /*
2069          * Don't bother with rename by filehandle unless file is busy and
2070          * source. Note that cross directory moves do not work with
2071          * rename by filehandle to various Windows servers.
2072          */
2073         if (rc == 0 || rc != -EBUSY)
2074                 goto do_rename_exit;
2075
2076         /* Don't fall back to using SMB on SMB 2+ mount */
2077         if (server->vals->protocol_id != 0)
2078                 goto do_rename_exit;
2079
2080 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2081         /* open-file renames don't work across directories */
2082         if (to_dentry->d_parent != from_dentry->d_parent)
2083                 goto do_rename_exit;
2084
2085         oparms.tcon = tcon;
2086         oparms.cifs_sb = cifs_sb;
2087         /* open the file to be renamed -- we need DELETE perms */
2088         oparms.desired_access = DELETE;
2089         oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
2090         oparms.disposition = FILE_OPEN;
2091         oparms.path = from_path;
2092         oparms.fid = &fid;
2093         oparms.reconnect = false;
2094
2095         rc = CIFS_open(xid, &oparms, &oplock, NULL);
2096         if (rc == 0) {
2097                 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2098                                 (const char *) to_dentry->d_name.name,
2099                                 cifs_sb->local_nls, cifs_remap(cifs_sb));
2100                 CIFSSMBClose(xid, tcon, fid.netfid);
2101         }
2102 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2103 do_rename_exit:
2104         if (rc == 0)
2105                 d_move(from_dentry, to_dentry);
2106         cifs_put_tlink(tlink);
2107         return rc;
2108 }
2109
2110 int
2111 cifs_rename2(struct user_namespace *mnt_userns, struct inode *source_dir,
2112              struct dentry *source_dentry, struct inode *target_dir,
2113              struct dentry *target_dentry, unsigned int flags)
2114 {
2115         const char *from_name, *to_name;
2116         void *page1, *page2;
2117         struct cifs_sb_info *cifs_sb;
2118         struct tcon_link *tlink;
2119         struct cifs_tcon *tcon;
2120         unsigned int xid;
2121         int rc, tmprc;
2122         int retry_count = 0;
2123         FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2124 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2125         FILE_UNIX_BASIC_INFO *info_buf_target;
2126 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2127
2128         if (flags & ~RENAME_NOREPLACE)
2129                 return -EINVAL;
2130
2131         cifs_sb = CIFS_SB(source_dir->i_sb);
2132         if (unlikely(cifs_forced_shutdown(cifs_sb)))
2133                 return -EIO;
2134
2135         tlink = cifs_sb_tlink(cifs_sb);
2136         if (IS_ERR(tlink))
2137                 return PTR_ERR(tlink);
2138         tcon = tlink_tcon(tlink);
2139
2140         page1 = alloc_dentry_path();
2141         page2 = alloc_dentry_path();
2142         xid = get_xid();
2143
2144         from_name = build_path_from_dentry(source_dentry, page1);
2145         if (IS_ERR(from_name)) {
2146                 rc = PTR_ERR(from_name);
2147                 goto cifs_rename_exit;
2148         }
2149
2150         to_name = build_path_from_dentry(target_dentry, page2);
2151         if (IS_ERR(to_name)) {
2152                 rc = PTR_ERR(to_name);
2153                 goto cifs_rename_exit;
2154         }
2155
2156         cifs_close_deferred_file_under_dentry(tcon, from_name);
2157         if (d_inode(target_dentry) != NULL)
2158                 cifs_close_deferred_file_under_dentry(tcon, to_name);
2159
2160         rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2161                             to_name);
2162
2163         if (rc == -EACCES) {
2164                 while (retry_count < 3) {
2165                         cifs_close_all_deferred_files(tcon);
2166                         rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2167                                             to_name);
2168                         if (rc != -EACCES)
2169                                 break;
2170                         retry_count++;
2171                 }
2172         }
2173
2174         /*
2175          * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2176          */
2177         if (flags & RENAME_NOREPLACE)
2178                 goto cifs_rename_exit;
2179
2180 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2181         if (rc == -EEXIST && tcon->unix_ext) {
2182                 /*
2183                  * Are src and dst hardlinks of same inode? We can only tell
2184                  * with unix extensions enabled.
2185                  */
2186                 info_buf_source =
2187                         kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2188                                         GFP_KERNEL);
2189                 if (info_buf_source == NULL) {
2190                         rc = -ENOMEM;
2191                         goto cifs_rename_exit;
2192                 }
2193
2194                 info_buf_target = info_buf_source + 1;
2195                 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2196                                              info_buf_source,
2197                                              cifs_sb->local_nls,
2198                                              cifs_remap(cifs_sb));
2199                 if (tmprc != 0)
2200                         goto unlink_target;
2201
2202                 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2203                                              info_buf_target,
2204                                              cifs_sb->local_nls,
2205                                              cifs_remap(cifs_sb));
2206
2207                 if (tmprc == 0 && (info_buf_source->UniqueId ==
2208                                    info_buf_target->UniqueId)) {
2209                         /* same file, POSIX says that this is a noop */
2210                         rc = 0;
2211                         goto cifs_rename_exit;
2212                 }
2213         }
2214         /*
2215          * else ... BB we could add the same check for Windows by
2216          * checking the UniqueId via FILE_INTERNAL_INFO
2217          */
2218
2219 unlink_target:
2220 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2221
2222         /* Try unlinking the target dentry if it's not negative */
2223         if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
2224                 if (d_is_dir(target_dentry))
2225                         tmprc = cifs_rmdir(target_dir, target_dentry);
2226                 else
2227                         tmprc = cifs_unlink(target_dir, target_dentry);
2228                 if (tmprc)
2229                         goto cifs_rename_exit;
2230                 rc = cifs_do_rename(xid, source_dentry, from_name,
2231                                     target_dentry, to_name);
2232         }
2233
2234         /* force revalidate to go get info when needed */
2235         CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2236
2237         source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
2238                 target_dir->i_mtime = current_time(source_dir);
2239
2240 cifs_rename_exit:
2241         kfree(info_buf_source);
2242         free_dentry_path(page2);
2243         free_dentry_path(page1);
2244         free_xid(xid);
2245         cifs_put_tlink(tlink);
2246         return rc;
2247 }
2248
2249 static bool
2250 cifs_dentry_needs_reval(struct dentry *dentry)
2251 {
2252         struct inode *inode = d_inode(dentry);
2253         struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2254         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2255         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2256         struct cached_fid *cfid = NULL;
2257
2258         if (cifs_i->time == 0)
2259                 return true;
2260
2261         if (CIFS_CACHE_READ(cifs_i))
2262                 return false;
2263
2264         if (!lookupCacheEnabled)
2265                 return true;
2266
2267         if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2268                 mutex_lock(&cfid->fid_mutex);
2269                 if (cfid->time && cifs_i->time > cfid->time) {
2270                         mutex_unlock(&cfid->fid_mutex);
2271                         close_cached_dir(cfid);
2272                         return false;
2273                 }
2274                 mutex_unlock(&cfid->fid_mutex);
2275                 close_cached_dir(cfid);
2276         }
2277         /*
2278          * depending on inode type, check if attribute caching disabled for
2279          * files or directories
2280          */
2281         if (S_ISDIR(inode->i_mode)) {
2282                 if (!cifs_sb->ctx->acdirmax)
2283                         return true;
2284                 if (!time_in_range(jiffies, cifs_i->time,
2285                                    cifs_i->time + cifs_sb->ctx->acdirmax))
2286                         return true;
2287         } else { /* file */
2288                 if (!cifs_sb->ctx->acregmax)
2289                         return true;
2290                 if (!time_in_range(jiffies, cifs_i->time,
2291                                    cifs_i->time + cifs_sb->ctx->acregmax))
2292                         return true;
2293         }
2294
2295         /* hardlinked files w/ noserverino get "special" treatment */
2296         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2297             S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2298                 return true;
2299
2300         return false;
2301 }
2302
2303 /*
2304  * Zap the cache. Called when invalid_mapping flag is set.
2305  */
2306 int
2307 cifs_invalidate_mapping(struct inode *inode)
2308 {
2309         int rc = 0;
2310
2311         if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
2312                 rc = invalidate_inode_pages2(inode->i_mapping);
2313                 if (rc)
2314                         cifs_dbg(VFS, "%s: Could not invalidate inode %p\n",
2315                                  __func__, inode);
2316         }
2317
2318         return rc;
2319 }
2320
2321 /**
2322  * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2323  *
2324  * @key:        currently unused
2325  * @mode:       the task state to sleep in
2326  */
2327 static int
2328 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2329 {
2330         schedule();
2331         if (signal_pending_state(mode, current))
2332                 return -ERESTARTSYS;
2333         return 0;
2334 }
2335
2336 int
2337 cifs_revalidate_mapping(struct inode *inode)
2338 {
2339         int rc;
2340         unsigned long *flags = &CIFS_I(inode)->flags;
2341         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2342
2343         /* swapfiles are not supposed to be shared */
2344         if (IS_SWAPFILE(inode))
2345                 return 0;
2346
2347         rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2348                                      TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2349         if (rc)
2350                 return rc;
2351
2352         if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2353                 /* for cache=singleclient, do not invalidate */
2354                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2355                         goto skip_invalidate;
2356
2357                 rc = cifs_invalidate_mapping(inode);
2358                 if (rc)
2359                         set_bit(CIFS_INO_INVALID_MAPPING, flags);
2360         }
2361
2362 skip_invalidate:
2363         clear_bit_unlock(CIFS_INO_LOCK, flags);
2364         smp_mb__after_atomic();
2365         wake_up_bit(flags, CIFS_INO_LOCK);
2366
2367         return rc;
2368 }
2369
2370 int
2371 cifs_zap_mapping(struct inode *inode)
2372 {
2373         set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2374         return cifs_revalidate_mapping(inode);
2375 }
2376
2377 int cifs_revalidate_file_attr(struct file *filp)
2378 {
2379         int rc = 0;
2380         struct dentry *dentry = file_dentry(filp);
2381 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2382         struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2383 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2384
2385         if (!cifs_dentry_needs_reval(dentry))
2386                 return rc;
2387
2388 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2389         if (tlink_tcon(cfile->tlink)->unix_ext)
2390                 rc = cifs_get_file_info_unix(filp);
2391         else
2392 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2393                 rc = cifs_get_file_info(filp);
2394
2395         return rc;
2396 }
2397
2398 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2399 {
2400         unsigned int xid;
2401         int rc = 0;
2402         struct inode *inode = d_inode(dentry);
2403         struct super_block *sb = dentry->d_sb;
2404         const char *full_path;
2405         void *page;
2406         int count = 0;
2407
2408         if (inode == NULL)
2409                 return -ENOENT;
2410
2411         if (!cifs_dentry_needs_reval(dentry))
2412                 return rc;
2413
2414         xid = get_xid();
2415
2416         page = alloc_dentry_path();
2417         full_path = build_path_from_dentry(dentry, page);
2418         if (IS_ERR(full_path)) {
2419                 rc = PTR_ERR(full_path);
2420                 goto out;
2421         }
2422
2423         cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2424                  full_path, inode, inode->i_count.counter,
2425                  dentry, cifs_get_time(dentry), jiffies);
2426
2427 again:
2428         if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
2429                 rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
2430         else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
2431                 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2432         else
2433                 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2434                                          xid, NULL);
2435         if (rc == -EAGAIN && count++ < 10)
2436                 goto again;
2437 out:
2438         free_dentry_path(page);
2439         free_xid(xid);
2440
2441         return rc;
2442 }
2443
2444 int cifs_revalidate_file(struct file *filp)
2445 {
2446         int rc;
2447         struct inode *inode = file_inode(filp);
2448
2449         rc = cifs_revalidate_file_attr(filp);
2450         if (rc)
2451                 return rc;
2452
2453         return cifs_revalidate_mapping(inode);
2454 }
2455
2456 /* revalidate a dentry's inode attributes */
2457 int cifs_revalidate_dentry(struct dentry *dentry)
2458 {
2459         int rc;
2460         struct inode *inode = d_inode(dentry);
2461
2462         rc = cifs_revalidate_dentry_attr(dentry);
2463         if (rc)
2464                 return rc;
2465
2466         return cifs_revalidate_mapping(inode);
2467 }
2468
2469 int cifs_getattr(struct user_namespace *mnt_userns, const struct path *path,
2470                  struct kstat *stat, u32 request_mask, unsigned int flags)
2471 {
2472         struct dentry *dentry = path->dentry;
2473         struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2474         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2475         struct inode *inode = d_inode(dentry);
2476         int rc;
2477
2478         if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2479                 return -EIO;
2480
2481         /*
2482          * We need to be sure that all dirty pages are written and the server
2483          * has actual ctime, mtime and file length.
2484          */
2485         if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2486             !CIFS_CACHE_READ(CIFS_I(inode)) &&
2487             inode->i_mapping && inode->i_mapping->nrpages != 0) {
2488                 rc = filemap_fdatawait(inode->i_mapping);
2489                 if (rc) {
2490                         mapping_set_error(inode->i_mapping, rc);
2491                         return rc;
2492                 }
2493         }
2494
2495         if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2496                 CIFS_I(inode)->time = 0; /* force revalidate */
2497
2498         /*
2499          * If the caller doesn't require syncing, only sync if
2500          * necessary (e.g. due to earlier truncate or setattr
2501          * invalidating the cached metadata)
2502          */
2503         if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2504             (CIFS_I(inode)->time == 0)) {
2505                 rc = cifs_revalidate_dentry_attr(dentry);
2506                 if (rc)
2507                         return rc;
2508         }
2509
2510         generic_fillattr(&init_user_ns, inode, stat);
2511         stat->blksize = cifs_sb->ctx->bsize;
2512         stat->ino = CIFS_I(inode)->uniqueid;
2513
2514         /* old CIFS Unix Extensions doesn't return create time */
2515         if (CIFS_I(inode)->createtime) {
2516                 stat->result_mask |= STATX_BTIME;
2517                 stat->btime =
2518                       cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2519         }
2520
2521         stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2522         if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2523                 stat->attributes |= STATX_ATTR_COMPRESSED;
2524         if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2525                 stat->attributes |= STATX_ATTR_ENCRYPTED;
2526
2527         /*
2528          * If on a multiuser mount without unix extensions or cifsacl being
2529          * enabled, and the admin hasn't overridden them, set the ownership
2530          * to the fsuid/fsgid of the current process.
2531          */
2532         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2533             !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2534             !tcon->unix_ext) {
2535                 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2536                         stat->uid = current_fsuid();
2537                 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2538                         stat->gid = current_fsgid();
2539         }
2540         return 0;
2541 }
2542
2543 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2544                 u64 len)
2545 {
2546         struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2547         struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2548         struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2549         struct TCP_Server_Info *server = tcon->ses->server;
2550         struct cifsFileInfo *cfile;
2551         int rc;
2552
2553         if (unlikely(cifs_forced_shutdown(cifs_sb)))
2554                 return -EIO;
2555
2556         /*
2557          * We need to be sure that all dirty pages are written as they
2558          * might fill holes on the server.
2559          */
2560         if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2561             inode->i_mapping->nrpages != 0) {
2562                 rc = filemap_fdatawait(inode->i_mapping);
2563                 if (rc) {
2564                         mapping_set_error(inode->i_mapping, rc);
2565                         return rc;
2566                 }
2567         }
2568
2569         cfile = find_readable_file(cifs_i, false);
2570         if (cfile == NULL)
2571                 return -EINVAL;
2572
2573         if (server->ops->fiemap) {
2574                 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
2575                 cifsFileInfo_put(cfile);
2576                 return rc;
2577         }
2578
2579         cifsFileInfo_put(cfile);
2580         return -ENOTSUPP;
2581 }
2582
2583 int cifs_truncate_page(struct address_space *mapping, loff_t from)
2584 {
2585         pgoff_t index = from >> PAGE_SHIFT;
2586         unsigned offset = from & (PAGE_SIZE - 1);
2587         struct page *page;
2588         int rc = 0;
2589
2590         page = grab_cache_page(mapping, index);
2591         if (!page)
2592                 return -ENOMEM;
2593
2594         zero_user_segment(page, offset, PAGE_SIZE);
2595         unlock_page(page);
2596         put_page(page);
2597         return rc;
2598 }
2599
2600 void cifs_setsize(struct inode *inode, loff_t offset)
2601 {
2602         struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2603
2604         spin_lock(&inode->i_lock);
2605         i_size_write(inode, offset);
2606         spin_unlock(&inode->i_lock);
2607
2608         /* Cached inode must be refreshed on truncate */
2609         cifs_i->time = 0;
2610         truncate_pagecache(inode, offset);
2611 }
2612
2613 static int
2614 cifs_set_file_size(struct inode *inode, struct iattr *attrs,
2615                    unsigned int xid, const char *full_path)
2616 {
2617         int rc;
2618         struct cifsFileInfo *open_file;
2619         struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2620         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2621         struct tcon_link *tlink = NULL;
2622         struct cifs_tcon *tcon = NULL;
2623         struct TCP_Server_Info *server;
2624
2625         /*
2626          * To avoid spurious oplock breaks from server, in the case of
2627          * inodes that we already have open, avoid doing path based
2628          * setting of file size if we can do it by handle.
2629          * This keeps our caching token (oplock) and avoids timeouts
2630          * when the local oplock break takes longer to flush
2631          * writebehind data than the SMB timeout for the SetPathInfo
2632          * request would allow
2633          */
2634         open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2635         if (open_file) {
2636                 tcon = tlink_tcon(open_file->tlink);
2637                 server = tcon->ses->server;
2638                 if (server->ops->set_file_size)
2639                         rc = server->ops->set_file_size(xid, tcon, open_file,
2640                                                         attrs->ia_size, false);
2641                 else
2642                         rc = -ENOSYS;
2643                 cifsFileInfo_put(open_file);
2644                 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
2645         } else
2646                 rc = -EINVAL;
2647
2648         if (!rc)
2649                 goto set_size_out;
2650
2651         if (tcon == NULL) {
2652                 tlink = cifs_sb_tlink(cifs_sb);
2653                 if (IS_ERR(tlink))
2654                         return PTR_ERR(tlink);
2655                 tcon = tlink_tcon(tlink);
2656                 server = tcon->ses->server;
2657         }
2658
2659         /*
2660          * Set file size by pathname rather than by handle either because no
2661          * valid, writeable file handle for it was found or because there was
2662          * an error setting it by handle.
2663          */
2664         if (server->ops->set_path_size)
2665                 rc = server->ops->set_path_size(xid, tcon, full_path,
2666                                                 attrs->ia_size, cifs_sb, false);
2667         else
2668                 rc = -ENOSYS;
2669         cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
2670
2671         if (tlink)
2672                 cifs_put_tlink(tlink);
2673
2674 set_size_out:
2675         if (rc == 0) {
2676                 cifsInode->server_eof = attrs->ia_size;
2677                 cifs_setsize(inode, attrs->ia_size);
2678                 /*
2679                  * i_blocks is not related to (i_size / i_blksize), but instead
2680                  * 512 byte (2**9) size is required for calculating num blocks.
2681                  * Until we can query the server for actual allocation size,
2682                  * this is best estimate we have for blocks allocated for a file
2683                  * Number of blocks must be rounded up so size 1 is not 0 blocks
2684                  */
2685                 inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
2686
2687                 /*
2688                  * The man page of truncate says if the size changed,
2689                  * then the st_ctime and st_mtime fields for the file
2690                  * are updated.
2691                  */
2692                 attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
2693                 attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
2694
2695                 cifs_truncate_page(inode->i_mapping, inode->i_size);
2696         }
2697
2698         return rc;
2699 }
2700
2701 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2702 static int
2703 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2704 {
2705         int rc;
2706         unsigned int xid;
2707         const char *full_path;
2708         void *page = alloc_dentry_path();
2709         struct inode *inode = d_inode(direntry);
2710         struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2711         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2712         struct tcon_link *tlink;
2713         struct cifs_tcon *pTcon;
2714         struct cifs_unix_set_info_args *args = NULL;
2715         struct cifsFileInfo *open_file;
2716
2717         cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2718                  direntry, attrs->ia_valid);
2719
2720         xid = get_xid();
2721
2722         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2723                 attrs->ia_valid |= ATTR_FORCE;
2724
2725         rc = setattr_prepare(&init_user_ns, direntry, attrs);
2726         if (rc < 0)
2727                 goto out;
2728
2729         full_path = build_path_from_dentry(direntry, page);
2730         if (IS_ERR(full_path)) {
2731                 rc = PTR_ERR(full_path);
2732                 goto out;
2733         }
2734
2735         /*
2736          * Attempt to flush data before changing attributes. We need to do
2737          * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2738          * ownership or mode then we may also need to do this. Here, we take
2739          * the safe way out and just do the flush on all setattr requests. If
2740          * the flush returns error, store it to report later and continue.
2741          *
2742          * BB: This should be smarter. Why bother flushing pages that
2743          * will be truncated anyway? Also, should we error out here if
2744          * the flush returns error?
2745          */
2746         rc = filemap_write_and_wait(inode->i_mapping);
2747         if (is_interrupt_error(rc)) {
2748                 rc = -ERESTARTSYS;
2749                 goto out;
2750         }
2751
2752         mapping_set_error(inode->i_mapping, rc);
2753         rc = 0;
2754
2755         if (attrs->ia_valid & ATTR_SIZE) {
2756                 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2757                 if (rc != 0)
2758                         goto out;
2759         }
2760
2761         /* skip mode change if it's just for clearing setuid/setgid */
2762         if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2763                 attrs->ia_valid &= ~ATTR_MODE;
2764
2765         args = kmalloc(sizeof(*args), GFP_KERNEL);
2766         if (args == NULL) {
2767                 rc = -ENOMEM;
2768                 goto out;
2769         }
2770
2771         /* set up the struct */
2772         if (attrs->ia_valid & ATTR_MODE)
2773                 args->mode = attrs->ia_mode;
2774         else
2775                 args->mode = NO_CHANGE_64;
2776
2777         if (attrs->ia_valid & ATTR_UID)
2778                 args->uid = attrs->ia_uid;
2779         else
2780                 args->uid = INVALID_UID; /* no change */
2781
2782         if (attrs->ia_valid & ATTR_GID)
2783                 args->gid = attrs->ia_gid;
2784         else
2785                 args->gid = INVALID_GID; /* no change */
2786
2787         if (attrs->ia_valid & ATTR_ATIME)
2788                 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2789         else
2790                 args->atime = NO_CHANGE_64;
2791
2792         if (attrs->ia_valid & ATTR_MTIME)
2793                 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2794         else
2795                 args->mtime = NO_CHANGE_64;
2796
2797         if (attrs->ia_valid & ATTR_CTIME)
2798                 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2799         else
2800                 args->ctime = NO_CHANGE_64;
2801
2802         args->device = 0;
2803         open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
2804         if (open_file) {
2805                 u16 nfid = open_file->fid.netfid;
2806                 u32 npid = open_file->pid;
2807                 pTcon = tlink_tcon(open_file->tlink);
2808                 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
2809                 cifsFileInfo_put(open_file);
2810         } else {
2811                 tlink = cifs_sb_tlink(cifs_sb);
2812                 if (IS_ERR(tlink)) {
2813                         rc = PTR_ERR(tlink);
2814                         goto out;
2815                 }
2816                 pTcon = tlink_tcon(tlink);
2817                 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
2818                                     cifs_sb->local_nls,
2819                                     cifs_remap(cifs_sb));
2820                 cifs_put_tlink(tlink);
2821         }
2822
2823         if (rc)
2824                 goto out;
2825
2826         if ((attrs->ia_valid & ATTR_SIZE) &&
2827             attrs->ia_size != i_size_read(inode)) {
2828                 truncate_setsize(inode, attrs->ia_size);
2829                 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
2830         }
2831
2832         setattr_copy(&init_user_ns, inode, attrs);
2833         mark_inode_dirty(inode);
2834
2835         /* force revalidate when any of these times are set since some
2836            of the fs types (eg ext3, fat) do not have fine enough
2837            time granularity to match protocol, and we do not have a
2838            a way (yet) to query the server fs's time granularity (and
2839            whether it rounds times down).
2840         */
2841         if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2842                 cifsInode->time = 0;
2843 out:
2844         kfree(args);
2845         free_dentry_path(page);
2846         free_xid(xid);
2847         return rc;
2848 }
2849 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2850
2851 static int
2852 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
2853 {
2854         unsigned int xid;
2855         kuid_t uid = INVALID_UID;
2856         kgid_t gid = INVALID_GID;
2857         struct inode *inode = d_inode(direntry);
2858         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2859         struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2860         struct cifsFileInfo *wfile;
2861         struct cifs_tcon *tcon;
2862         const char *full_path;
2863         void *page = alloc_dentry_path();
2864         int rc = -EACCES;
2865         __u32 dosattr = 0;
2866         __u64 mode = NO_CHANGE_64;
2867
2868         xid = get_xid();
2869
2870         cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
2871                  direntry, attrs->ia_valid);
2872
2873         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2874                 attrs->ia_valid |= ATTR_FORCE;
2875
2876         rc = setattr_prepare(&init_user_ns, direntry, attrs);
2877         if (rc < 0)
2878                 goto cifs_setattr_exit;
2879
2880         full_path = build_path_from_dentry(direntry, page);
2881         if (IS_ERR(full_path)) {
2882                 rc = PTR_ERR(full_path);
2883                 goto cifs_setattr_exit;
2884         }
2885
2886         /*
2887          * Attempt to flush data before changing attributes. We need to do
2888          * this for ATTR_SIZE and ATTR_MTIME.  If the flush of the data
2889          * returns error, store it to report later and continue.
2890          *
2891          * BB: This should be smarter. Why bother flushing pages that
2892          * will be truncated anyway? Also, should we error out here if
2893          * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
2894          */
2895         if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
2896                 rc = filemap_write_and_wait(inode->i_mapping);
2897                 if (is_interrupt_error(rc)) {
2898                         rc = -ERESTARTSYS;
2899                         goto cifs_setattr_exit;
2900                 }
2901                 mapping_set_error(inode->i_mapping, rc);
2902         }
2903
2904         rc = 0;
2905
2906         if ((attrs->ia_valid & ATTR_MTIME) &&
2907             !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
2908                 rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
2909                 if (!rc) {
2910                         tcon = tlink_tcon(wfile->tlink);
2911                         rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
2912                         cifsFileInfo_put(wfile);
2913                         if (rc)
2914                                 goto cifs_setattr_exit;
2915                 } else if (rc != -EBADF)
2916                         goto cifs_setattr_exit;
2917                 else
2918                         rc = 0;
2919         }
2920
2921         if (attrs->ia_valid & ATTR_SIZE) {
2922                 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2923                 if (rc != 0)
2924                         goto cifs_setattr_exit;
2925         }
2926
2927         if (attrs->ia_valid & ATTR_UID)
2928                 uid = attrs->ia_uid;
2929
2930         if (attrs->ia_valid & ATTR_GID)
2931                 gid = attrs->ia_gid;
2932
2933         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2934             (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2935                 if (uid_valid(uid) || gid_valid(gid)) {
2936                         mode = NO_CHANGE_64;
2937                         rc = id_mode_to_cifs_acl(inode, full_path, &mode,
2938                                                         uid, gid);
2939                         if (rc) {
2940                                 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2941                                          __func__, rc);
2942                                 goto cifs_setattr_exit;
2943                         }
2944                 }
2945         } else
2946         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
2947                 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
2948
2949         /* skip mode change if it's just for clearing setuid/setgid */
2950         if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2951                 attrs->ia_valid &= ~ATTR_MODE;
2952
2953         if (attrs->ia_valid & ATTR_MODE) {
2954                 mode = attrs->ia_mode;
2955                 rc = 0;
2956                 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
2957                     (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
2958                         rc = id_mode_to_cifs_acl(inode, full_path, &mode,
2959                                                 INVALID_UID, INVALID_GID);
2960                         if (rc) {
2961                                 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2962                                          __func__, rc);
2963                                 goto cifs_setattr_exit;
2964                         }
2965
2966                         /*
2967                          * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
2968                          * Pick up the actual mode bits that were set.
2969                          */
2970                         if (mode != attrs->ia_mode)
2971                                 attrs->ia_mode = mode;
2972                 } else
2973                 if (((mode & S_IWUGO) == 0) &&
2974                     (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
2975
2976                         dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2977
2978                         /* fix up mode if we're not using dynperm */
2979                         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2980                                 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2981                 } else if ((mode & S_IWUGO) &&
2982                            (cifsInode->cifsAttrs & ATTR_READONLY)) {
2983
2984                         dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2985                         /* Attributes of 0 are ignored */
2986                         if (dosattr == 0)
2987                                 dosattr |= ATTR_NORMAL;
2988
2989                         /* reset local inode permissions to normal */
2990                         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2991                                 attrs->ia_mode &= ~(S_IALLUGO);
2992                                 if (S_ISDIR(inode->i_mode))
2993                                         attrs->ia_mode |=
2994                                                 cifs_sb->ctx->dir_mode;
2995                                 else
2996                                         attrs->ia_mode |=
2997                                                 cifs_sb->ctx->file_mode;
2998                         }
2999                 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3000                         /* ignore mode change - ATTR_READONLY hasn't changed */
3001                         attrs->ia_valid &= ~ATTR_MODE;
3002                 }
3003         }
3004
3005         if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3006             ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3007                 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3008                 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3009
3010                 /* Even if error on time set, no sense failing the call if
3011                 the server would set the time to a reasonable value anyway,
3012                 and this check ensures that we are not being called from
3013                 sys_utimes in which case we ought to fail the call back to
3014                 the user when the server rejects the call */
3015                 if ((rc) && (attrs->ia_valid &
3016                                 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3017                         rc = 0;
3018         }
3019
3020         /* do not need local check to inode_check_ok since the server does
3021            that */
3022         if (rc)
3023                 goto cifs_setattr_exit;
3024
3025         if ((attrs->ia_valid & ATTR_SIZE) &&
3026             attrs->ia_size != i_size_read(inode)) {
3027                 truncate_setsize(inode, attrs->ia_size);
3028                 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3029         }
3030
3031         setattr_copy(&init_user_ns, inode, attrs);
3032         mark_inode_dirty(inode);
3033
3034 cifs_setattr_exit:
3035         free_xid(xid);
3036         free_dentry_path(page);
3037         return rc;
3038 }
3039
3040 int
3041 cifs_setattr(struct user_namespace *mnt_userns, struct dentry *direntry,
3042              struct iattr *attrs)
3043 {
3044         struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3045         int rc, retries = 0;
3046 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3047         struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3048 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3049
3050         if (unlikely(cifs_forced_shutdown(cifs_sb)))
3051                 return -EIO;
3052
3053         do {
3054 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3055                 if (pTcon->unix_ext)
3056                         rc = cifs_setattr_unix(direntry, attrs);
3057                 else
3058 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3059                         rc = cifs_setattr_nounix(direntry, attrs);
3060                 retries++;
3061         } while (is_retryable_error(rc) && retries < 2);
3062
3063         /* BB: add cifs_setattr_legacy for really old servers */
3064         return rc;
3065 }