[CIFS] Enable DFS support for Unix query path info
[pandora-kernel.git] / fs / cifs / inode.c
1 /*
2  *   fs/cifs/inode.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2007
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/pagemap.h>
24 #include <asm/div64.h>
25 #include "cifsfs.h"
26 #include "cifspdu.h"
27 #include "cifsglob.h"
28 #include "cifsproto.h"
29 #include "cifs_debug.h"
30 #include "cifs_fs_sb.h"
31
32
33 static void cifs_set_ops(struct inode *inode, const bool is_dfs_referral)
34 {
35         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
36
37         switch (inode->i_mode & S_IFMT) {
38         case S_IFREG:
39                 inode->i_op = &cifs_file_inode_ops;
40                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
41                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
42                                 inode->i_fop = &cifs_file_direct_nobrl_ops;
43                         else
44                                 inode->i_fop = &cifs_file_direct_ops;
45                 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
46                         inode->i_fop = &cifs_file_nobrl_ops;
47                 else { /* not direct, send byte range locks */
48                         inode->i_fop = &cifs_file_ops;
49                 }
50
51
52                 /* check if server can support readpages */
53                 if (cifs_sb->tcon->ses->server->maxBuf <
54                                 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
55                         inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
56                 else
57                         inode->i_data.a_ops = &cifs_addr_ops;
58                 break;
59         case S_IFDIR:
60 #ifdef CONFIG_CIFS_DFS_UPCALL
61                 if (is_dfs_referral) {
62                         inode->i_op = &cifs_dfs_referral_inode_operations;
63                 } else {
64 #else /* NO DFS support, treat as a directory */
65                 {
66 #endif
67                         inode->i_op = &cifs_dir_inode_ops;
68                         inode->i_fop = &cifs_dir_ops;
69                 }
70                 break;
71         case S_IFLNK:
72                 inode->i_op = &cifs_symlink_inode_ops;
73                 break;
74         default:
75                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
76                 break;
77         }
78 }
79
80 static void cifs_unix_info_to_inode(struct inode *inode,
81                 FILE_UNIX_BASIC_INFO *info, int force_uid_gid)
82 {
83         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
84         struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
85         __u64 num_of_bytes = le64_to_cpu(info->NumOfBytes);
86         __u64 end_of_file = le64_to_cpu(info->EndOfFile);
87
88         inode->i_atime = cifs_NTtimeToUnix(le64_to_cpu(info->LastAccessTime));
89         inode->i_mtime =
90                 cifs_NTtimeToUnix(le64_to_cpu(info->LastModificationTime));
91         inode->i_ctime = cifs_NTtimeToUnix(le64_to_cpu(info->LastStatusChange));
92         inode->i_mode = le64_to_cpu(info->Permissions);
93
94         /*
95          * Since we set the inode type below we need to mask off
96          * to avoid strange results if bits set above.
97          */
98         inode->i_mode &= ~S_IFMT;
99         switch (le32_to_cpu(info->Type)) {
100         case UNIX_FILE:
101                 inode->i_mode |= S_IFREG;
102                 break;
103         case UNIX_SYMLINK:
104                 inode->i_mode |= S_IFLNK;
105                 break;
106         case UNIX_DIR:
107                 inode->i_mode |= S_IFDIR;
108                 break;
109         case UNIX_CHARDEV:
110                 inode->i_mode |= S_IFCHR;
111                 inode->i_rdev = MKDEV(le64_to_cpu(info->DevMajor),
112                                       le64_to_cpu(info->DevMinor) & MINORMASK);
113                 break;
114         case UNIX_BLOCKDEV:
115                 inode->i_mode |= S_IFBLK;
116                 inode->i_rdev = MKDEV(le64_to_cpu(info->DevMajor),
117                                       le64_to_cpu(info->DevMinor) & MINORMASK);
118                 break;
119         case UNIX_FIFO:
120                 inode->i_mode |= S_IFIFO;
121                 break;
122         case UNIX_SOCKET:
123                 inode->i_mode |= S_IFSOCK;
124                 break;
125         default:
126                 /* safest to call it a file if we do not know */
127                 inode->i_mode |= S_IFREG;
128                 cFYI(1, ("unknown type %d", le32_to_cpu(info->Type)));
129                 break;
130         }
131
132         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) &&
133             !force_uid_gid)
134                 inode->i_uid = cifs_sb->mnt_uid;
135         else
136                 inode->i_uid = le64_to_cpu(info->Uid);
137
138         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) &&
139             !force_uid_gid)
140                 inode->i_gid = cifs_sb->mnt_gid;
141         else
142                 inode->i_gid = le64_to_cpu(info->Gid);
143
144         inode->i_nlink = le64_to_cpu(info->Nlinks);
145
146         spin_lock(&inode->i_lock);
147         if (is_size_safe_to_change(cifsInfo, end_of_file)) {
148                 /*
149                  * We can not safely change the file size here if the client
150                  * is writing to it due to potential races.
151                  */
152                 i_size_write(inode, end_of_file);
153
154                 /*
155                  * i_blocks is not related to (i_size / i_blksize),
156                  * but instead 512 byte (2**9) size is required for
157                  * calculating num blocks.
158                  */
159                 inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
160         }
161         spin_unlock(&inode->i_lock);
162 }
163
164 static void fill_fake_finddataunix(FILE_UNIX_BASIC_INFO *pfnd_dat,
165                                struct super_block *sb)
166 {
167         struct inode *pinode = NULL;
168
169         memset(pfnd_dat, sizeof(FILE_UNIX_BASIC_INFO), 0);
170
171 /*      __le64 pfnd_dat->EndOfFile = cpu_to_le64(0);
172         __le64 pfnd_dat->NumOfBytes = cpu_to_le64(0);
173         __u64 UniqueId = 0;  */
174         pfnd_dat->LastStatusChange =
175                 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
176         pfnd_dat->LastAccessTime =
177                 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
178         pfnd_dat->LastModificationTime =
179                 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
180         pfnd_dat->Type = cpu_to_le32(UNIX_DIR);
181         pfnd_dat->Permissions = cpu_to_le64(S_IXUGO | S_IRWXU);
182         pfnd_dat->Nlinks = cpu_to_le64(2);
183         if (sb->s_root)
184                 pinode = sb->s_root->d_inode;
185         if (pinode == NULL)
186                 return;
187
188         /* fill in default values for the remaining based on root
189            inode since we can not query the server for this inode info */
190         pfnd_dat->DevMajor = cpu_to_le64(MAJOR(pinode->i_rdev));
191         pfnd_dat->DevMinor = cpu_to_le64(MINOR(pinode->i_rdev));
192         pfnd_dat->Uid = cpu_to_le64(pinode->i_uid);
193         pfnd_dat->Gid = cpu_to_le64(pinode->i_gid);
194 }
195
196 int cifs_get_inode_info_unix(struct inode **pinode,
197         const unsigned char *full_path, struct super_block *sb, int xid)
198 {
199         int rc = 0;
200         FILE_UNIX_BASIC_INFO find_data;
201         struct cifsTconInfo *pTcon;
202         struct inode *inode;
203         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
204         bool is_dfs_referral = false;
205         struct cifsInodeInfo *cifsInfo;
206         __u64 num_of_bytes;
207         __u64 end_of_file;
208
209         pTcon = cifs_sb->tcon;
210         cFYI(1, ("Getting info on %s", full_path));
211
212         /* could have done a find first instead but this returns more info */
213         rc = CIFSSMBUnixQPathInfo(xid, pTcon, full_path, &find_data,
214                                   cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
215                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
216         if (rc) {
217                 if (rc == -EREMOTE && !is_dfs_referral) {
218                         is_dfs_referral = true;
219                         cERROR(1, ("DFS ref")); /* BB removeme BB */
220                         /* for DFS, server does not give us real inode data */
221                         fill_fake_finddataunix(&find_data, sb);
222                         rc = 0;
223                 }
224         }
225         num_of_bytes = le64_to_cpu(find_data.NumOfBytes);
226         end_of_file = le64_to_cpu(find_data.EndOfFile);
227
228         /* get new inode */
229         if (*pinode == NULL) {
230                 *pinode = new_inode(sb);
231                 if (*pinode == NULL) {
232                         rc = -ENOMEM;
233                 goto cgiiu_exit;
234                 }
235                 /* Is an i_ino of zero legal? */
236                 /* note ino incremented to unique num in new_inode */
237                 /* Are there sanity checks we can use to ensure that
238                    the server is really filling in that field? */
239                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
240                         (*pinode)->i_ino = (unsigned long)find_data.UniqueId;
241
242                 if (sb->s_flags & MS_NOATIME)
243                         (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
244
245                 insert_inode_hash(*pinode);
246         }
247
248         inode = *pinode;
249         cifsInfo = CIFS_I(inode);
250
251         cFYI(1, ("Old time %ld", cifsInfo->time));
252         cifsInfo->time = jiffies;
253         cFYI(1, ("New time %ld", cifsInfo->time));
254         /* this is ok to set on every inode revalidate */
255         atomic_set(&cifsInfo->inUse, 1);
256
257         cifs_unix_info_to_inode(inode, &find_data, 0);
258
259         if (num_of_bytes < end_of_file)
260                 cFYI(1, ("allocation size less than end of file"));
261         cFYI(1, ("Size %ld and blocks %llu",
262                 (unsigned long) inode->i_size,
263                 (unsigned long long)inode->i_blocks));
264
265         cifs_set_ops(inode, is_dfs_referral);
266 cgiiu_exit:
267         return rc;
268 }
269
270 static int decode_sfu_inode(struct inode *inode, __u64 size,
271                             const unsigned char *path,
272                             struct cifs_sb_info *cifs_sb, int xid)
273 {
274         int rc;
275         int oplock = 0;
276         __u16 netfid;
277         struct cifsTconInfo *pTcon = cifs_sb->tcon;
278         char buf[24];
279         unsigned int bytes_read;
280         char *pbuf;
281
282         pbuf = buf;
283
284         if (size == 0) {
285                 inode->i_mode |= S_IFIFO;
286                 return 0;
287         } else if (size < 8) {
288                 return -EINVAL;  /* EOPNOTSUPP? */
289         }
290
291         rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
292                          CREATE_NOT_DIR, &netfid, &oplock, NULL,
293                          cifs_sb->local_nls,
294                          cifs_sb->mnt_cifs_flags &
295                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
296         if (rc == 0) {
297                 int buf_type = CIFS_NO_BUFFER;
298                         /* Read header */
299                 rc = CIFSSMBRead(xid, pTcon,
300                                  netfid,
301                                  24 /* length */, 0 /* offset */,
302                                  &bytes_read, &pbuf, &buf_type);
303                 if ((rc == 0) && (bytes_read >= 8)) {
304                         if (memcmp("IntxBLK", pbuf, 8) == 0) {
305                                 cFYI(1, ("Block device"));
306                                 inode->i_mode |= S_IFBLK;
307                                 if (bytes_read == 24) {
308                                         /* we have enough to decode dev num */
309                                         __u64 mjr; /* major */
310                                         __u64 mnr; /* minor */
311                                         mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
312                                         mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
313                                         inode->i_rdev = MKDEV(mjr, mnr);
314                                 }
315                         } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
316                                 cFYI(1, ("Char device"));
317                                 inode->i_mode |= S_IFCHR;
318                                 if (bytes_read == 24) {
319                                         /* we have enough to decode dev num */
320                                         __u64 mjr; /* major */
321                                         __u64 mnr; /* minor */
322                                         mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
323                                         mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
324                                         inode->i_rdev = MKDEV(mjr, mnr);
325                                 }
326                         } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
327                                 cFYI(1, ("Symlink"));
328                                 inode->i_mode |= S_IFLNK;
329                         } else {
330                                 inode->i_mode |= S_IFREG; /* file? */
331                                 rc = -EOPNOTSUPP;
332                         }
333                 } else {
334                         inode->i_mode |= S_IFREG; /* then it is a file */
335                         rc = -EOPNOTSUPP; /* or some unknown SFU type */
336                 }
337                 CIFSSMBClose(xid, pTcon, netfid);
338         }
339         return rc;
340 }
341
342 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID)  /* SETFILEBITS valid bits */
343
344 static int get_sfu_mode(struct inode *inode,
345                         const unsigned char *path,
346                         struct cifs_sb_info *cifs_sb, int xid)
347 {
348 #ifdef CONFIG_CIFS_XATTR
349         ssize_t rc;
350         char ea_value[4];
351         __u32 mode;
352
353         rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
354                         ea_value, 4 /* size of buf */, cifs_sb->local_nls,
355                 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
356         if (rc < 0)
357                 return (int)rc;
358         else if (rc > 3) {
359                 mode = le32_to_cpu(*((__le32 *)ea_value));
360                 inode->i_mode &= ~SFBITS_MASK;
361                 cFYI(1, ("special bits 0%o org mode 0%o", mode, inode->i_mode));
362                 inode->i_mode = (mode &  SFBITS_MASK) | inode->i_mode;
363                 cFYI(1, ("special mode bits 0%o", mode));
364                 return 0;
365         } else {
366                 return 0;
367         }
368 #else
369         return -EOPNOTSUPP;
370 #endif
371 }
372
373 int cifs_get_inode_info(struct inode **pinode,
374         const unsigned char *full_path, FILE_ALL_INFO *pfindData,
375         struct super_block *sb, int xid, const __u16 *pfid)
376 {
377         int rc = 0;
378         struct cifsTconInfo *pTcon;
379         struct inode *inode;
380         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
381         char *buf = NULL;
382         bool adjustTZ = false;
383         bool is_dfs_referral = false;
384
385         pTcon = cifs_sb->tcon;
386         cFYI(1, ("Getting info on %s", full_path));
387
388         if ((pfindData == NULL) && (*pinode != NULL)) {
389                 if (CIFS_I(*pinode)->clientCanCacheRead) {
390                         cFYI(1, ("No need to revalidate cached inode sizes"));
391                         return rc;
392                 }
393         }
394
395         /* if file info not passed in then get it from server */
396         if (pfindData == NULL) {
397                 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
398                 if (buf == NULL)
399                         return -ENOMEM;
400                 pfindData = (FILE_ALL_INFO *)buf;
401
402 try_again_CIFSSMBQPathInfo:
403                 /* could do find first instead but this returns more info */
404                 rc = CIFSSMBQPathInfo(xid, pTcon, full_path, pfindData,
405                               0 /* not legacy */,
406                               cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
407                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
408                 /* BB optimize code so we do not make the above call
409                 when server claims no NT SMB support and the above call
410                 failed at least once - set flag in tcon or mount */
411                 if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
412                         rc = SMBQueryInformation(xid, pTcon, full_path,
413                                         pfindData, cifs_sb->local_nls,
414                                         cifs_sb->mnt_cifs_flags &
415                                           CIFS_MOUNT_MAP_SPECIAL_CHR);
416                         adjustTZ = true;
417                 }
418         }
419         /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
420         if (rc) {
421                 if (rc == -EREMOTE && !is_dfs_referral) {
422                         is_dfs_referral = true;
423                         goto try_again_CIFSSMBQPathInfo;
424                 }
425                 goto cgii_exit;
426         } else {
427                 struct cifsInodeInfo *cifsInfo;
428                 __u32 attr = le32_to_cpu(pfindData->Attributes);
429
430                 /* get new inode */
431                 if (*pinode == NULL) {
432                         *pinode = new_inode(sb);
433                         if (*pinode == NULL) {
434                                 rc = -ENOMEM;
435                                 goto cgii_exit;
436                         }
437                         /* Is an i_ino of zero legal? Can we use that to check
438                            if the server supports returning inode numbers?  Are
439                            there other sanity checks we can use to ensure that
440                            the server is really filling in that field? */
441
442                         /* We can not use the IndexNumber field by default from
443                            Windows or Samba (in ALL_INFO buf) but we can request
444                            it explicitly.  It may not be unique presumably if
445                            the server has multiple devices mounted under one
446                            share */
447
448                         /* There may be higher info levels that work but are
449                            there Windows server or network appliances for which
450                            IndexNumber field is not guaranteed unique? */
451
452                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
453                                 int rc1 = 0;
454                                 __u64 inode_num;
455
456                                 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
457                                         full_path, &inode_num,
458                                         cifs_sb->local_nls,
459                                         cifs_sb->mnt_cifs_flags &
460                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
461                                 if (rc1) {
462                                         cFYI(1, ("GetSrvInodeNum rc %d", rc1));
463                                         /* BB EOPNOSUPP disable SERVER_INUM? */
464                                 } else /* do we need cast or hash to ino? */
465                                         (*pinode)->i_ino = inode_num;
466                         } /* else ino incremented to unique num in new_inode*/
467                         if (sb->s_flags & MS_NOATIME)
468                                 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
469                         insert_inode_hash(*pinode);
470                 }
471                 inode = *pinode;
472                 cifsInfo = CIFS_I(inode);
473                 cifsInfo->cifsAttrs = attr;
474                 cFYI(1, ("Old time %ld", cifsInfo->time));
475                 cifsInfo->time = jiffies;
476                 cFYI(1, ("New time %ld", cifsInfo->time));
477
478                 /* blksize needs to be multiple of two. So safer to default to
479                 blksize and blkbits set in superblock so 2**blkbits and blksize
480                 will match rather than setting to:
481                 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
482
483                 /* Linux can not store file creation time so ignore it */
484                 if (pfindData->LastAccessTime)
485                         inode->i_atime = cifs_NTtimeToUnix
486                                 (le64_to_cpu(pfindData->LastAccessTime));
487                 else /* do not need to use current_fs_time - time not stored */
488                         inode->i_atime = CURRENT_TIME;
489                 inode->i_mtime =
490                     cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
491                 inode->i_ctime =
492                     cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
493                 cFYI(0, ("Attributes came in as 0x%x", attr));
494                 if (adjustTZ && (pTcon->ses) && (pTcon->ses->server)) {
495                         inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj;
496                         inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj;
497                 }
498
499                 /* set default mode. will override for dirs below */
500                 if (atomic_read(&cifsInfo->inUse) == 0)
501                         /* new inode, can safely set these fields */
502                         inode->i_mode = cifs_sb->mnt_file_mode;
503                 else /* since we set the inode type below we need to mask off
504                      to avoid strange results if type changes and both
505                      get orred in */
506                         inode->i_mode &= ~S_IFMT;
507 /*              if (attr & ATTR_REPARSE)  */
508                 /* We no longer handle these as symlinks because we could not
509                    follow them due to the absolute path with drive letter */
510                 if (attr & ATTR_DIRECTORY) {
511                 /* override default perms since we do not do byte range locking
512                    on dirs */
513                         inode->i_mode = cifs_sb->mnt_dir_mode;
514                         inode->i_mode |= S_IFDIR;
515                 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
516                            (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
517                            /* No need to le64 convert size of zero */
518                            (pfindData->EndOfFile == 0)) {
519                         inode->i_mode = cifs_sb->mnt_file_mode;
520                         inode->i_mode |= S_IFIFO;
521 /* BB Finish for SFU style symlinks and devices */
522                 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
523                            (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
524                         if (decode_sfu_inode(inode,
525                                          le64_to_cpu(pfindData->EndOfFile),
526                                          full_path,
527                                          cifs_sb, xid))
528                                 cFYI(1, ("Unrecognized sfu inode type"));
529
530                         cFYI(1, ("sfu mode 0%o", inode->i_mode));
531                 } else {
532                         inode->i_mode |= S_IFREG;
533                         /* treat the dos attribute of read-only as read-only
534                            mode e.g. 555 */
535                         if (cifsInfo->cifsAttrs & ATTR_READONLY)
536                                 inode->i_mode &= ~(S_IWUGO);
537                         else if ((inode->i_mode & S_IWUGO) == 0)
538                                 /* the ATTR_READONLY flag may have been */
539                                 /* changed on server -- set any w bits  */
540                                 /* allowed by mnt_file_mode             */
541                                 inode->i_mode |= (S_IWUGO &
542                                                   cifs_sb->mnt_file_mode);
543                 /* BB add code here -
544                    validate if device or weird share or device type? */
545                 }
546
547                 spin_lock(&inode->i_lock);
548                 if (is_size_safe_to_change(cifsInfo,
549                                            le64_to_cpu(pfindData->EndOfFile))) {
550                         /* can not safely shrink the file size here if the
551                            client is writing to it due to potential races */
552                         i_size_write(inode, le64_to_cpu(pfindData->EndOfFile));
553
554                         /* 512 bytes (2**9) is the fake blocksize that must be
555                            used for this calculation */
556                         inode->i_blocks = (512 - 1 + le64_to_cpu(
557                                            pfindData->AllocationSize)) >> 9;
558                 }
559                 spin_unlock(&inode->i_lock);
560
561                 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
562
563                 /* BB fill in uid and gid here? with help from winbind?
564                    or retrieve from NTFS stream extended attribute */
565 #ifdef CONFIG_CIFS_EXPERIMENTAL
566                 /* fill in 0777 bits from ACL */
567                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
568                         cFYI(1, ("Getting mode bits from ACL"));
569                         acl_to_uid_mode(inode, full_path, pfid);
570                 }
571 #endif
572                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
573                         /* fill in remaining high mode bits e.g. SUID, VTX */
574                         get_sfu_mode(inode, full_path, cifs_sb, xid);
575                 } else if (atomic_read(&cifsInfo->inUse) == 0) {
576                         inode->i_uid = cifs_sb->mnt_uid;
577                         inode->i_gid = cifs_sb->mnt_gid;
578                         /* set so we do not keep refreshing these fields with
579                            bad data after user has changed them in memory */
580                         atomic_set(&cifsInfo->inUse, 1);
581                 }
582
583                 cifs_set_ops(inode, is_dfs_referral);
584         }
585 cgii_exit:
586         kfree(buf);
587         return rc;
588 }
589
590 static const struct inode_operations cifs_ipc_inode_ops = {
591         .lookup = cifs_lookup,
592 };
593
594 /* gets root inode */
595 struct inode *cifs_iget(struct super_block *sb, unsigned long ino)
596 {
597         int xid;
598         struct cifs_sb_info *cifs_sb;
599         struct inode *inode;
600         long rc;
601
602         inode = iget_locked(sb, ino);
603         if (!inode)
604                 return ERR_PTR(-ENOMEM);
605         if (!(inode->i_state & I_NEW))
606                 return inode;
607
608         cifs_sb = CIFS_SB(inode->i_sb);
609         xid = GetXid();
610
611         if (cifs_sb->tcon->unix_ext)
612                 rc = cifs_get_inode_info_unix(&inode, "", inode->i_sb, xid);
613         else
614                 rc = cifs_get_inode_info(&inode, "", NULL, inode->i_sb, xid,
615                                          NULL);
616         if (rc && cifs_sb->tcon->ipc) {
617                 cFYI(1, ("ipc connection - fake read inode"));
618                 inode->i_mode |= S_IFDIR;
619                 inode->i_nlink = 2;
620                 inode->i_op = &cifs_ipc_inode_ops;
621                 inode->i_fop = &simple_dir_operations;
622                 inode->i_uid = cifs_sb->mnt_uid;
623                 inode->i_gid = cifs_sb->mnt_gid;
624                 _FreeXid(xid);
625                 iget_failed(inode);
626                 return ERR_PTR(rc);
627         }
628
629         unlock_new_inode(inode);
630
631         /* can not call macro FreeXid here since in a void func
632          * TODO: This is no longer true
633          */
634         _FreeXid(xid);
635         return inode;
636 }
637
638 int cifs_unlink(struct inode *inode, struct dentry *direntry)
639 {
640         int rc = 0;
641         int xid;
642         struct cifs_sb_info *cifs_sb;
643         struct cifsTconInfo *pTcon;
644         char *full_path = NULL;
645         struct cifsInodeInfo *cifsInode;
646         FILE_BASIC_INFO *pinfo_buf;
647
648         cFYI(1, ("cifs_unlink, inode = 0x%p", inode));
649
650         xid = GetXid();
651
652         if (inode)
653                 cifs_sb = CIFS_SB(inode->i_sb);
654         else
655                 cifs_sb = CIFS_SB(direntry->d_sb);
656         pTcon = cifs_sb->tcon;
657
658         /* Unlink can be called from rename so we can not grab the sem here
659            since we deadlock otherwise */
660 /*      mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);*/
661         full_path = build_path_from_dentry(direntry);
662 /*      mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);*/
663         if (full_path == NULL) {
664                 FreeXid(xid);
665                 return -ENOMEM;
666         }
667
668         if ((pTcon->ses->capabilities & CAP_UNIX) &&
669                 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
670                         le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
671                 rc = CIFSPOSIXDelFile(xid, pTcon, full_path,
672                         SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
673                         cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
674                 cFYI(1, ("posix del rc %d", rc));
675                 if ((rc == 0) || (rc == -ENOENT))
676                         goto psx_del_no_retry;
677         }
678
679         rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
680                         cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
681 psx_del_no_retry:
682         if (!rc) {
683                 if (direntry->d_inode)
684                         drop_nlink(direntry->d_inode);
685         } else if (rc == -ENOENT) {
686                 d_drop(direntry);
687         } else if (rc == -ETXTBSY) {
688                 int oplock = 0;
689                 __u16 netfid;
690
691                 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
692                                  CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
693                                  &netfid, &oplock, NULL, cifs_sb->local_nls,
694                                  cifs_sb->mnt_cifs_flags &
695                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
696                 if (rc == 0) {
697                         CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
698                                               cifs_sb->local_nls,
699                                               cifs_sb->mnt_cifs_flags &
700                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
701                         CIFSSMBClose(xid, pTcon, netfid);
702                         if (direntry->d_inode)
703                                 drop_nlink(direntry->d_inode);
704                 }
705         } else if (rc == -EACCES) {
706                 /* try only if r/o attribute set in local lookup data? */
707                 pinfo_buf = kzalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
708                 if (pinfo_buf) {
709                         /* ATTRS set to normal clears r/o bit */
710                         pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
711                         if (!(pTcon->ses->flags & CIFS_SES_NT4))
712                                 rc = CIFSSMBSetTimes(xid, pTcon, full_path,
713                                                      pinfo_buf,
714                                                      cifs_sb->local_nls,
715                                                      cifs_sb->mnt_cifs_flags &
716                                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
717                         else
718                                 rc = -EOPNOTSUPP;
719
720                         if (rc == -EOPNOTSUPP) {
721                                 int oplock = 0;
722                                 __u16 netfid;
723                         /*      rc = CIFSSMBSetAttrLegacy(xid, pTcon,
724                                                           full_path,
725                                                           (__u16)ATTR_NORMAL,
726                                                           cifs_sb->local_nls);
727                            For some strange reason it seems that NT4 eats the
728                            old setattr call without actually setting the
729                            attributes so on to the third attempted workaround
730                            */
731
732                         /* BB could scan to see if we already have it open
733                            and pass in pid of opener to function */
734                                 rc = CIFSSMBOpen(xid, pTcon, full_path,
735                                                  FILE_OPEN, SYNCHRONIZE |
736                                                  FILE_WRITE_ATTRIBUTES, 0,
737                                                  &netfid, &oplock, NULL,
738                                                  cifs_sb->local_nls,
739                                                  cifs_sb->mnt_cifs_flags &
740                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
741                                 if (rc == 0) {
742                                         rc = CIFSSMBSetFileTimes(xid, pTcon,
743                                                                  pinfo_buf,
744                                                                  netfid);
745                                         CIFSSMBClose(xid, pTcon, netfid);
746                                 }
747                         }
748                         kfree(pinfo_buf);
749                 }
750                 if (rc == 0) {
751                         rc = CIFSSMBDelFile(xid, pTcon, full_path,
752                                             cifs_sb->local_nls,
753                                             cifs_sb->mnt_cifs_flags &
754                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
755                         if (!rc) {
756                                 if (direntry->d_inode)
757                                         drop_nlink(direntry->d_inode);
758                         } else if (rc == -ETXTBSY) {
759                                 int oplock = 0;
760                                 __u16 netfid;
761
762                                 rc = CIFSSMBOpen(xid, pTcon, full_path,
763                                                  FILE_OPEN, DELETE,
764                                                  CREATE_NOT_DIR |
765                                                  CREATE_DELETE_ON_CLOSE,
766                                                  &netfid, &oplock, NULL,
767                                                  cifs_sb->local_nls,
768                                                  cifs_sb->mnt_cifs_flags &
769                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
770                                 if (rc == 0) {
771                                         CIFSSMBRenameOpenFile(xid, pTcon,
772                                                 netfid, NULL,
773                                                 cifs_sb->local_nls,
774                                                 cifs_sb->mnt_cifs_flags &
775                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
776                                         CIFSSMBClose(xid, pTcon, netfid);
777                                         if (direntry->d_inode)
778                                                 drop_nlink(direntry->d_inode);
779                                 }
780                         /* BB if rc = -ETXTBUSY goto the rename logic BB */
781                         }
782                 }
783         }
784         if (direntry->d_inode) {
785                 cifsInode = CIFS_I(direntry->d_inode);
786                 cifsInode->time = 0;    /* will force revalidate to get info
787                                            when needed */
788                 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
789         }
790         if (inode) {
791                 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
792                 cifsInode = CIFS_I(inode);
793                 cifsInode->time = 0;    /* force revalidate of dir as well */
794         }
795
796         kfree(full_path);
797         FreeXid(xid);
798         return rc;
799 }
800
801 static void posix_fill_in_inode(struct inode *tmp_inode,
802         FILE_UNIX_BASIC_INFO *pData, int isNewInode)
803 {
804         struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
805         loff_t local_size;
806         struct timespec local_mtime;
807
808         cifsInfo->time = jiffies;
809         atomic_inc(&cifsInfo->inUse);
810
811         /* save mtime and size */
812         local_mtime = tmp_inode->i_mtime;
813         local_size  = tmp_inode->i_size;
814
815         cifs_unix_info_to_inode(tmp_inode, pData, 1);
816         cifs_set_ops(tmp_inode, false);
817
818         if (!S_ISREG(tmp_inode->i_mode))
819                 return;
820
821         /*
822          * No sense invalidating pages for new inode
823          * since we we have not started caching
824          * readahead file data yet.
825          */
826         if (isNewInode)
827                 return;
828
829         if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
830                 (local_size == tmp_inode->i_size)) {
831                 cFYI(1, ("inode exists but unchanged"));
832         } else {
833                 /* file may have changed on server */
834                 cFYI(1, ("invalidate inode, readdir detected change"));
835                 invalidate_remote_inode(tmp_inode);
836         }
837 }
838
839 int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
840 {
841         int rc = 0;
842         int xid;
843         struct cifs_sb_info *cifs_sb;
844         struct cifsTconInfo *pTcon;
845         char *full_path = NULL;
846         struct inode *newinode = NULL;
847
848         cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
849
850         xid = GetXid();
851
852         cifs_sb = CIFS_SB(inode->i_sb);
853         pTcon = cifs_sb->tcon;
854
855         full_path = build_path_from_dentry(direntry);
856         if (full_path == NULL) {
857                 FreeXid(xid);
858                 return -ENOMEM;
859         }
860
861         if ((pTcon->ses->capabilities & CAP_UNIX) &&
862                 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
863                         le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
864                 u32 oplock = 0;
865                 FILE_UNIX_BASIC_INFO *pInfo =
866                         kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
867                 if (pInfo == NULL) {
868                         rc = -ENOMEM;
869                         goto mkdir_out;
870                 }
871
872                 mode &= ~current->fs->umask;
873                 rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT,
874                                 mode, NULL /* netfid */, pInfo, &oplock,
875                                 full_path, cifs_sb->local_nls,
876                                 cifs_sb->mnt_cifs_flags &
877                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
878                 if (rc == -EOPNOTSUPP) {
879                         kfree(pInfo);
880                         goto mkdir_retry_old;
881                 } else if (rc) {
882                         cFYI(1, ("posix mkdir returned 0x%x", rc));
883                         d_drop(direntry);
884                 } else {
885                         if (pInfo->Type == cpu_to_le32(-1)) {
886                                 /* no return info, go query for it */
887                                 kfree(pInfo);
888                                 goto mkdir_get_info;
889                         }
890 /*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need
891         to set uid/gid */
892                         inc_nlink(inode);
893                         if (pTcon->nocase)
894                                 direntry->d_op = &cifs_ci_dentry_ops;
895                         else
896                                 direntry->d_op = &cifs_dentry_ops;
897
898                         newinode = new_inode(inode->i_sb);
899                         if (newinode == NULL) {
900                                 kfree(pInfo);
901                                 goto mkdir_get_info;
902                         }
903                         /* Is an i_ino of zero legal? */
904                         /* Are there sanity checks we can use to ensure that
905                            the server is really filling in that field? */
906                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
907                                 newinode->i_ino =
908                                         (unsigned long)pInfo->UniqueId;
909                         } /* note ino incremented to unique num in new_inode */
910                         if (inode->i_sb->s_flags & MS_NOATIME)
911                                 newinode->i_flags |= S_NOATIME | S_NOCMTIME;
912                         newinode->i_nlink = 2;
913
914                         insert_inode_hash(newinode);
915                         d_instantiate(direntry, newinode);
916
917                         /* we already checked in POSIXCreate whether
918                            frame was long enough */
919                         posix_fill_in_inode(direntry->d_inode,
920                                         pInfo, 1 /* NewInode */);
921 #ifdef CONFIG_CIFS_DEBUG2
922                         cFYI(1, ("instantiated dentry %p %s to inode %p",
923                                 direntry, direntry->d_name.name, newinode));
924
925                         if (newinode->i_nlink != 2)
926                                 cFYI(1, ("unexpected number of links %d",
927                                         newinode->i_nlink));
928 #endif
929                 }
930                 kfree(pInfo);
931                 goto mkdir_out;
932         }
933 mkdir_retry_old:
934         /* BB add setting the equivalent of mode via CreateX w/ACLs */
935         rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
936                           cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
937         if (rc) {
938                 cFYI(1, ("cifs_mkdir returned 0x%x", rc));
939                 d_drop(direntry);
940         } else {
941 mkdir_get_info:
942                 inc_nlink(inode);
943                 if (pTcon->unix_ext)
944                         rc = cifs_get_inode_info_unix(&newinode, full_path,
945                                                       inode->i_sb, xid);
946                 else
947                         rc = cifs_get_inode_info(&newinode, full_path, NULL,
948                                                  inode->i_sb, xid, NULL);
949
950                 if (pTcon->nocase)
951                         direntry->d_op = &cifs_ci_dentry_ops;
952                 else
953                         direntry->d_op = &cifs_dentry_ops;
954                 d_instantiate(direntry, newinode);
955                  /* setting nlink not necessary except in cases where we
956                   * failed to get it from the server or was set bogus */
957                 if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2))
958                                 direntry->d_inode->i_nlink = 2;
959                 mode &= ~current->fs->umask;
960                 if (pTcon->unix_ext) {
961                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
962                                 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
963                                                     mode,
964                                                     (__u64)current->fsuid,
965                                                     (__u64)current->fsgid,
966                                                     0 /* dev_t */,
967                                                     cifs_sb->local_nls,
968                                                     cifs_sb->mnt_cifs_flags &
969                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
970                         } else {
971                                 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
972                                                     mode, (__u64)-1,
973                                                     (__u64)-1, 0 /* dev_t */,
974                                                     cifs_sb->local_nls,
975                                                     cifs_sb->mnt_cifs_flags &
976                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
977                         }
978                 } else {
979                         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
980                             (mode & S_IWUGO) == 0) {
981                                 FILE_BASIC_INFO pInfo;
982                                 memset(&pInfo, 0, sizeof(pInfo));
983                                 pInfo.Attributes = cpu_to_le32(ATTR_READONLY);
984                                 CIFSSMBSetTimes(xid, pTcon, full_path,
985                                                 &pInfo, cifs_sb->local_nls,
986                                                 cifs_sb->mnt_cifs_flags &
987                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
988                         }
989                         if (direntry->d_inode) {
990                                 direntry->d_inode->i_mode = mode;
991                                 direntry->d_inode->i_mode |= S_IFDIR;
992                                 if (cifs_sb->mnt_cifs_flags &
993                                      CIFS_MOUNT_SET_UID) {
994                                         direntry->d_inode->i_uid =
995                                                 current->fsuid;
996                                         direntry->d_inode->i_gid =
997                                                 current->fsgid;
998                                 }
999                         }
1000                 }
1001         }
1002 mkdir_out:
1003         kfree(full_path);
1004         FreeXid(xid);
1005         return rc;
1006 }
1007
1008 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1009 {
1010         int rc = 0;
1011         int xid;
1012         struct cifs_sb_info *cifs_sb;
1013         struct cifsTconInfo *pTcon;
1014         char *full_path = NULL;
1015         struct cifsInodeInfo *cifsInode;
1016
1017         cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
1018
1019         xid = GetXid();
1020
1021         cifs_sb = CIFS_SB(inode->i_sb);
1022         pTcon = cifs_sb->tcon;
1023
1024         full_path = build_path_from_dentry(direntry);
1025         if (full_path == NULL) {
1026                 FreeXid(xid);
1027                 return -ENOMEM;
1028         }
1029
1030         rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
1031                           cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1032
1033         if (!rc) {
1034                 drop_nlink(inode);
1035                 spin_lock(&direntry->d_inode->i_lock);
1036                 i_size_write(direntry->d_inode, 0);
1037                 clear_nlink(direntry->d_inode);
1038                 spin_unlock(&direntry->d_inode->i_lock);
1039         }
1040
1041         cifsInode = CIFS_I(direntry->d_inode);
1042         cifsInode->time = 0;    /* force revalidate to go get info when
1043                                    needed */
1044         direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
1045                 current_fs_time(inode->i_sb);
1046
1047         kfree(full_path);
1048         FreeXid(xid);
1049         return rc;
1050 }
1051
1052 int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
1053         struct inode *target_inode, struct dentry *target_direntry)
1054 {
1055         char *fromName;
1056         char *toName;
1057         struct cifs_sb_info *cifs_sb_source;
1058         struct cifs_sb_info *cifs_sb_target;
1059         struct cifsTconInfo *pTcon;
1060         int xid;
1061         int rc = 0;
1062
1063         xid = GetXid();
1064
1065         cifs_sb_target = CIFS_SB(target_inode->i_sb);
1066         cifs_sb_source = CIFS_SB(source_inode->i_sb);
1067         pTcon = cifs_sb_source->tcon;
1068
1069         if (pTcon != cifs_sb_target->tcon) {
1070                 FreeXid(xid);
1071                 return -EXDEV;  /* BB actually could be allowed if same server,
1072                                    but different share.
1073                                    Might eventually add support for this */
1074         }
1075
1076         /* we already  have the rename sem so we do not need to grab it again
1077            here to protect the path integrity */
1078         fromName = build_path_from_dentry(source_direntry);
1079         toName = build_path_from_dentry(target_direntry);
1080         if ((fromName == NULL) || (toName == NULL)) {
1081                 rc = -ENOMEM;
1082                 goto cifs_rename_exit;
1083         }
1084
1085         rc = CIFSSMBRename(xid, pTcon, fromName, toName,
1086                            cifs_sb_source->local_nls,
1087                            cifs_sb_source->mnt_cifs_flags &
1088                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1089         if (rc == -EEXIST) {
1090                 /* check if they are the same file because rename of hardlinked
1091                    files is a noop */
1092                 FILE_UNIX_BASIC_INFO *info_buf_source;
1093                 FILE_UNIX_BASIC_INFO *info_buf_target;
1094
1095                 info_buf_source =
1096                         kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1097                 if (info_buf_source != NULL) {
1098                         info_buf_target = info_buf_source + 1;
1099                         if (pTcon->unix_ext)
1100                                 rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
1101                                         info_buf_source,
1102                                         cifs_sb_source->local_nls,
1103                                         cifs_sb_source->mnt_cifs_flags &
1104                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1105                         /* else rc is still EEXIST so will fall through to
1106                            unlink the target and retry rename */
1107                         if (rc == 0) {
1108                                 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
1109                                                 info_buf_target,
1110                                                 cifs_sb_target->local_nls,
1111                                                 /* remap based on source sb */
1112                                                 cifs_sb_source->mnt_cifs_flags &
1113                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
1114                         }
1115                         if ((rc == 0) &&
1116                             (info_buf_source->UniqueId ==
1117                              info_buf_target->UniqueId)) {
1118                         /* do not rename since the files are hardlinked which
1119                            is a noop */
1120                         } else {
1121                         /* we either can not tell the files are hardlinked
1122                            (as with Windows servers) or files are not
1123                            hardlinked so delete the target manually before
1124                            renaming to follow POSIX rather than Windows
1125                            semantics */
1126                                 cifs_unlink(target_inode, target_direntry);
1127                                 rc = CIFSSMBRename(xid, pTcon, fromName,
1128                                                    toName,
1129                                                    cifs_sb_source->local_nls,
1130                                                    cifs_sb_source->mnt_cifs_flags
1131                                                    & CIFS_MOUNT_MAP_SPECIAL_CHR);
1132                         }
1133                         kfree(info_buf_source);
1134                 } /* if we can not get memory just leave rc as EEXIST */
1135         }
1136
1137         if (rc)
1138                 cFYI(1, ("rename rc %d", rc));
1139
1140         if ((rc == -EIO) || (rc == -EEXIST)) {
1141                 int oplock = 0;
1142                 __u16 netfid;
1143
1144                 /* BB FIXME Is Generic Read correct for rename? */
1145                 /* if renaming directory - we should not say CREATE_NOT_DIR,
1146                    need to test renaming open directory, also GENERIC_READ
1147                    might not right be right access to request */
1148                 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
1149                                  CREATE_NOT_DIR, &netfid, &oplock, NULL,
1150                                  cifs_sb_source->local_nls,
1151                                  cifs_sb_source->mnt_cifs_flags &
1152                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
1153                 if (rc == 0) {
1154                         rc = CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
1155                                               cifs_sb_source->local_nls,
1156                                               cifs_sb_source->mnt_cifs_flags &
1157                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1158                         CIFSSMBClose(xid, pTcon, netfid);
1159                 }
1160         }
1161
1162 cifs_rename_exit:
1163         kfree(fromName);
1164         kfree(toName);
1165         FreeXid(xid);
1166         return rc;
1167 }
1168
1169 int cifs_revalidate(struct dentry *direntry)
1170 {
1171         int xid;
1172         int rc = 0, wbrc = 0;
1173         char *full_path;
1174         struct cifs_sb_info *cifs_sb;
1175         struct cifsInodeInfo *cifsInode;
1176         loff_t local_size;
1177         struct timespec local_mtime;
1178         bool invalidate_inode = false;
1179
1180         if (direntry->d_inode == NULL)
1181                 return -ENOENT;
1182
1183         cifsInode = CIFS_I(direntry->d_inode);
1184
1185         if (cifsInode == NULL)
1186                 return -ENOENT;
1187
1188         /* no sense revalidating inode info on file that no one can write */
1189         if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
1190                 return rc;
1191
1192         xid = GetXid();
1193
1194         cifs_sb = CIFS_SB(direntry->d_sb);
1195
1196         /* can not safely grab the rename sem here if rename calls revalidate
1197            since that would deadlock */
1198         full_path = build_path_from_dentry(direntry);
1199         if (full_path == NULL) {
1200                 FreeXid(xid);
1201                 return -ENOMEM;
1202         }
1203         cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
1204                  "jiffies %ld", full_path, direntry->d_inode,
1205                  direntry->d_inode->i_count.counter, direntry,
1206                  direntry->d_time, jiffies));
1207
1208         if (cifsInode->time == 0) {
1209                 /* was set to zero previously to force revalidate */
1210         } else if (time_before(jiffies, cifsInode->time + HZ) &&
1211                    lookupCacheEnabled) {
1212                 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
1213                     (direntry->d_inode->i_nlink == 1)) {
1214                         kfree(full_path);
1215                         FreeXid(xid);
1216                         return rc;
1217                 } else {
1218                         cFYI(1, ("Have to revalidate file due to hardlinks"));
1219                 }
1220         }
1221
1222         /* save mtime and size */
1223         local_mtime = direntry->d_inode->i_mtime;
1224         local_size = direntry->d_inode->i_size;
1225
1226         if (cifs_sb->tcon->unix_ext) {
1227                 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
1228                                               direntry->d_sb, xid);
1229                 if (rc) {
1230                         cFYI(1, ("error on getting revalidate info %d", rc));
1231 /*                      if (rc != -ENOENT)
1232                                 rc = 0; */      /* BB should we cache info on
1233                                                    certain errors? */
1234                 }
1235         } else {
1236                 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
1237                                          direntry->d_sb, xid, NULL);
1238                 if (rc) {
1239                         cFYI(1, ("error on getting revalidate info %d", rc));
1240 /*                      if (rc != -ENOENT)
1241                                 rc = 0; */      /* BB should we cache info on
1242                                                    certain errors? */
1243                 }
1244         }
1245         /* should we remap certain errors, access denied?, to zero */
1246
1247         /* if not oplocked, we invalidate inode pages if mtime or file size
1248            had changed on server */
1249
1250         if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) &&
1251             (local_size == direntry->d_inode->i_size)) {
1252                 cFYI(1, ("cifs_revalidate - inode unchanged"));
1253         } else {
1254                 /* file may have changed on server */
1255                 if (cifsInode->clientCanCacheRead) {
1256                         /* no need to invalidate inode pages since we were the
1257                            only ones who could have modified the file and the
1258                            server copy is staler than ours */
1259                 } else {
1260                         invalidate_inode = true;
1261                 }
1262         }
1263
1264         /* can not grab this sem since kernel filesys locking documentation
1265            indicates i_mutex may be taken by the kernel on lookup and rename
1266            which could deadlock if we grab the i_mutex here as well */
1267 /*      mutex_lock(&direntry->d_inode->i_mutex);*/
1268         /* need to write out dirty pages here  */
1269         if (direntry->d_inode->i_mapping) {
1270                 /* do we need to lock inode until after invalidate completes
1271                    below? */
1272                 wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping);
1273                 if (wbrc)
1274                         CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
1275         }
1276         if (invalidate_inode) {
1277         /* shrink_dcache not necessary now that cifs dentry ops
1278         are exported for negative dentries */
1279 /*              if (S_ISDIR(direntry->d_inode->i_mode))
1280                         shrink_dcache_parent(direntry); */
1281                 if (S_ISREG(direntry->d_inode->i_mode)) {
1282                         if (direntry->d_inode->i_mapping)
1283                                 wbrc = filemap_fdatawait(direntry->d_inode->i_mapping);
1284                                 if (wbrc)
1285                                         CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
1286                         /* may eventually have to do this for open files too */
1287                         if (list_empty(&(cifsInode->openFileList))) {
1288                                 /* changed on server - flush read ahead pages */
1289                                 cFYI(1, ("Invalidating read ahead data on "
1290                                          "closed file"));
1291                                 invalidate_remote_inode(direntry->d_inode);
1292                         }
1293                 }
1294         }
1295 /*      mutex_unlock(&direntry->d_inode->i_mutex); */
1296
1297         kfree(full_path);
1298         FreeXid(xid);
1299         return rc;
1300 }
1301
1302 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1303         struct kstat *stat)
1304 {
1305         int err = cifs_revalidate(dentry);
1306         if (!err) {
1307                 generic_fillattr(dentry->d_inode, stat);
1308                 stat->blksize = CIFS_MAX_MSGSIZE;
1309         }
1310         return err;
1311 }
1312
1313 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1314 {
1315         pgoff_t index = from >> PAGE_CACHE_SHIFT;
1316         unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1317         struct page *page;
1318         int rc = 0;
1319
1320         page = grab_cache_page(mapping, index);
1321         if (!page)
1322                 return -ENOMEM;
1323
1324         zero_user_segment(page, offset, PAGE_CACHE_SIZE);
1325         unlock_page(page);
1326         page_cache_release(page);
1327         return rc;
1328 }
1329
1330 static int cifs_vmtruncate(struct inode *inode, loff_t offset)
1331 {
1332         struct address_space *mapping = inode->i_mapping;
1333         unsigned long limit;
1334
1335         spin_lock(&inode->i_lock);
1336         if (inode->i_size < offset)
1337                 goto do_expand;
1338         /*
1339          * truncation of in-use swapfiles is disallowed - it would cause
1340          * subsequent swapout to scribble on the now-freed blocks.
1341          */
1342         if (IS_SWAPFILE(inode)) {
1343                 spin_unlock(&inode->i_lock);
1344                 goto out_busy;
1345         }
1346         i_size_write(inode, offset);
1347         spin_unlock(&inode->i_lock);
1348         /*
1349          * unmap_mapping_range is called twice, first simply for efficiency
1350          * so that truncate_inode_pages does fewer single-page unmaps. However
1351          * after this first call, and before truncate_inode_pages finishes,
1352          * it is possible for private pages to be COWed, which remain after
1353          * truncate_inode_pages finishes, hence the second unmap_mapping_range
1354          * call must be made for correctness.
1355          */
1356         unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1357         truncate_inode_pages(mapping, offset);
1358         unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1359         goto out_truncate;
1360
1361 do_expand:
1362         limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1363         if (limit != RLIM_INFINITY && offset > limit) {
1364                 spin_unlock(&inode->i_lock);
1365                 goto out_sig;
1366         }
1367         if (offset > inode->i_sb->s_maxbytes) {
1368                 spin_unlock(&inode->i_lock);
1369                 goto out_big;
1370         }
1371         i_size_write(inode, offset);
1372         spin_unlock(&inode->i_lock);
1373 out_truncate:
1374         if (inode->i_op && inode->i_op->truncate)
1375                 inode->i_op->truncate(inode);
1376         return 0;
1377 out_sig:
1378         send_sig(SIGXFSZ, current, 0);
1379 out_big:
1380         return -EFBIG;
1381 out_busy:
1382         return -ETXTBSY;
1383 }
1384
1385 int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
1386 {
1387         int xid;
1388         struct cifs_sb_info *cifs_sb;
1389         struct cifsTconInfo *pTcon;
1390         char *full_path = NULL;
1391         int rc = -EACCES;
1392         struct cifsFileInfo *open_file = NULL;
1393         FILE_BASIC_INFO time_buf;
1394         bool set_time = false;
1395         bool set_dosattr = false;
1396         __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
1397         __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
1398         __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
1399         struct cifsInodeInfo *cifsInode;
1400         struct inode *inode = direntry->d_inode;
1401
1402         xid = GetXid();
1403
1404         cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
1405                  direntry->d_name.name, attrs->ia_valid));
1406
1407         cifs_sb = CIFS_SB(inode->i_sb);
1408         pTcon = cifs_sb->tcon;
1409
1410         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
1411                 /* check if we have permission to change attrs */
1412                 rc = inode_change_ok(inode, attrs);
1413                 if (rc < 0) {
1414                         FreeXid(xid);
1415                         return rc;
1416                 } else
1417                         rc = 0;
1418         }
1419
1420         full_path = build_path_from_dentry(direntry);
1421         if (full_path == NULL) {
1422                 FreeXid(xid);
1423                 return -ENOMEM;
1424         }
1425         cifsInode = CIFS_I(inode);
1426
1427         if ((attrs->ia_valid & ATTR_MTIME) || (attrs->ia_valid & ATTR_SIZE)) {
1428                 /*
1429                    Flush data before changing file size or changing the last
1430                    write time of the file on the server. If the
1431                    flush returns error, store it to report later and continue.
1432                    BB: This should be smarter. Why bother flushing pages that
1433                    will be truncated anyway? Also, should we error out here if
1434                    the flush returns error?
1435                  */
1436                 rc = filemap_write_and_wait(inode->i_mapping);
1437                 if (rc != 0) {
1438                         cifsInode->write_behind_rc = rc;
1439                         rc = 0;
1440                 }
1441         }
1442
1443         if (attrs->ia_valid & ATTR_SIZE) {
1444                 /* To avoid spurious oplock breaks from server, in the case of
1445                    inodes that we already have open, avoid doing path based
1446                    setting of file size if we can do it by handle.
1447                    This keeps our caching token (oplock) and avoids timeouts
1448                    when the local oplock break takes longer to flush
1449                    writebehind data than the SMB timeout for the SetPathInfo
1450                    request would allow */
1451
1452                 open_file = find_writable_file(cifsInode);
1453                 if (open_file) {
1454                         __u16 nfid = open_file->netfid;
1455                         __u32 npid = open_file->pid;
1456                         rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
1457                                                 nfid, npid, false);
1458                         atomic_dec(&open_file->wrtPending);
1459                         cFYI(1, ("SetFSize for attrs rc = %d", rc));
1460                         if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1461                                 unsigned int bytes_written;
1462                                 rc = CIFSSMBWrite(xid, pTcon,
1463                                                   nfid, 0, attrs->ia_size,
1464                                                   &bytes_written, NULL, NULL,
1465                                                   1 /* 45 seconds */);
1466                                 cFYI(1, ("Wrt seteof rc %d", rc));
1467                         }
1468                 } else
1469                         rc = -EINVAL;
1470
1471                 if (rc != 0) {
1472                         /* Set file size by pathname rather than by handle
1473                            either because no valid, writeable file handle for
1474                            it was found or because there was an error setting
1475                            it by handle */
1476                         rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1477                                            attrs->ia_size, false,
1478                                            cifs_sb->local_nls,
1479                                            cifs_sb->mnt_cifs_flags &
1480                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1481                         cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1482                         if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1483                                 __u16 netfid;
1484                                 int oplock = 0;
1485
1486                                 rc = SMBLegacyOpen(xid, pTcon, full_path,
1487                                         FILE_OPEN, GENERIC_WRITE,
1488                                         CREATE_NOT_DIR, &netfid, &oplock,
1489                                         NULL, cifs_sb->local_nls,
1490                                         cifs_sb->mnt_cifs_flags &
1491                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1492                                 if (rc == 0) {
1493                                         unsigned int bytes_written;
1494                                         rc = CIFSSMBWrite(xid, pTcon,
1495                                                         netfid, 0,
1496                                                         attrs->ia_size,
1497                                                         &bytes_written, NULL,
1498                                                         NULL, 1 /* 45 sec */);
1499                                         cFYI(1, ("wrt seteof rc %d", rc));
1500                                         CIFSSMBClose(xid, pTcon, netfid);
1501                                 }
1502
1503                         }
1504                 }
1505
1506                 /* Server is ok setting allocation size implicitly - no need
1507                    to call:
1508                 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, true,
1509                          cifs_sb->local_nls);
1510                    */
1511
1512                 if (rc == 0) {
1513                         rc = cifs_vmtruncate(inode, attrs->ia_size);
1514                         cifs_truncate_page(inode->i_mapping, inode->i_size);
1515                 } else
1516                         goto cifs_setattr_exit;
1517         }
1518         if (attrs->ia_valid & ATTR_UID) {
1519                 cFYI(1, ("UID changed to %d", attrs->ia_uid));
1520                 uid = attrs->ia_uid;
1521         }
1522         if (attrs->ia_valid & ATTR_GID) {
1523                 cFYI(1, ("GID changed to %d", attrs->ia_gid));
1524                 gid = attrs->ia_gid;
1525         }
1526
1527         time_buf.Attributes = 0;
1528
1529         /* skip mode change if it's just for clearing setuid/setgid */
1530         if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1531                 attrs->ia_valid &= ~ATTR_MODE;
1532
1533         if (attrs->ia_valid & ATTR_MODE) {
1534                 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
1535                 mode = attrs->ia_mode;
1536         }
1537
1538         if ((pTcon->unix_ext)
1539             && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
1540                 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
1541                                          0 /* dev_t */, cifs_sb->local_nls,
1542                                          cifs_sb->mnt_cifs_flags &
1543                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1544         else if (attrs->ia_valid & ATTR_MODE) {
1545                 rc = 0;
1546 #ifdef CONFIG_CIFS_EXPERIMENTAL
1547                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
1548                         rc = mode_to_acl(inode, full_path, mode);
1549                 else if ((mode & S_IWUGO) == 0) {
1550 #else
1551                 if ((mode & S_IWUGO) == 0) {
1552 #endif
1553                         /* not writeable */
1554                         if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
1555                                 set_dosattr = true;
1556                                 time_buf.Attributes =
1557                                         cpu_to_le32(cifsInode->cifsAttrs |
1558                                                     ATTR_READONLY);
1559                         }
1560                 } else if (cifsInode->cifsAttrs & ATTR_READONLY) {
1561                         /* If file is readonly on server, we would
1562                         not be able to write to it - so if any write
1563                         bit is enabled for user or group or other we
1564                         need to at least try to remove r/o dos attr */
1565                         set_dosattr = true;
1566                         time_buf.Attributes = cpu_to_le32(cifsInode->cifsAttrs &
1567                                             (~ATTR_READONLY));
1568                         /* Windows ignores set to zero */
1569                         if (time_buf.Attributes == 0)
1570                                 time_buf.Attributes |= cpu_to_le32(ATTR_NORMAL);
1571                 }
1572         }
1573
1574         if (attrs->ia_valid & ATTR_ATIME) {
1575                 set_time = true;
1576                 time_buf.LastAccessTime =
1577                     cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1578         } else
1579                 time_buf.LastAccessTime = 0;
1580
1581         if (attrs->ia_valid & ATTR_MTIME) {
1582                 set_time = true;
1583                 time_buf.LastWriteTime =
1584                     cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1585         } else
1586                 time_buf.LastWriteTime = 0;
1587         /* Do not set ctime explicitly unless other time
1588            stamps are changed explicitly (i.e. by utime()
1589            since we would then have a mix of client and
1590            server times */
1591
1592         if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1593                 set_time = true;
1594                 /* Although Samba throws this field away
1595                 it may be useful to Windows - but we do
1596                 not want to set ctime unless some other
1597                 timestamp is changing */
1598                 cFYI(1, ("CIFS - CTIME changed"));
1599                 time_buf.ChangeTime =
1600                     cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1601         } else
1602                 time_buf.ChangeTime = 0;
1603
1604         if (set_time || set_dosattr) {
1605                 time_buf.CreationTime = 0;      /* do not change */
1606                 /* In the future we should experiment - try setting timestamps
1607                    via Handle (SetFileInfo) instead of by path */
1608                 if (!(pTcon->ses->flags & CIFS_SES_NT4))
1609                         rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
1610                                              cifs_sb->local_nls,
1611                                              cifs_sb->mnt_cifs_flags &
1612                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1613                 else
1614                         rc = -EOPNOTSUPP;
1615
1616                 if (rc == -EOPNOTSUPP) {
1617                         int oplock = 0;
1618                         __u16 netfid;
1619
1620                         cFYI(1, ("calling SetFileInfo since SetPathInfo for "
1621                                  "times not supported by this server"));
1622                         /* BB we could scan to see if we already have it open
1623                            and pass in pid of opener to function */
1624                         rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1625                                          SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1626                                          CREATE_NOT_DIR, &netfid, &oplock,
1627                                          NULL, cifs_sb->local_nls,
1628                                          cifs_sb->mnt_cifs_flags &
1629                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1630                         if (rc == 0) {
1631                                 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1632                                                          netfid);
1633                                 CIFSSMBClose(xid, pTcon, netfid);
1634                         } else {
1635                         /* BB For even older servers we could convert time_buf
1636                            into old DOS style which uses two second
1637                            granularity */
1638
1639                         /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
1640                                         &time_buf, cifs_sb->local_nls); */
1641                         }
1642                 }
1643                 /* Even if error on time set, no sense failing the call if
1644                 the server would set the time to a reasonable value anyway,
1645                 and this check ensures that we are not being called from
1646                 sys_utimes in which case we ought to fail the call back to
1647                 the user when the server rejects the call */
1648                 if ((rc) && (attrs->ia_valid &
1649                          (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
1650                         rc = 0;
1651         }
1652
1653         /* do not need local check to inode_check_ok since the server does
1654            that */
1655         if (!rc)
1656                 rc = inode_setattr(inode, attrs);
1657 cifs_setattr_exit:
1658         kfree(full_path);
1659         FreeXid(xid);
1660         return rc;
1661 }
1662
1663 #if 0
1664 void cifs_delete_inode(struct inode *inode)
1665 {
1666         cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
1667         /* may have to add back in if and when safe distributed caching of
1668            directories added e.g. via FindNotify */
1669 }
1670 #endif