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