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