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