~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/smb/client/inode.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php