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