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