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