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