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