Merge to Fedora kernel-2.6.18-1.2239_FC5 patched with stable patch-2.6.18.2-vs2.0...
[linux-2.6.git] / fs / cifs / inode.c
1 /*
2  *   fs/cifs/inode.c
3  *
4  *   Copyright (C) International Business Machines  Corp., 2002,2005
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/buffer_head.h>
23 #include <linux/stat.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
33 int cifs_get_inode_info_unix(struct inode **pinode,
34         const unsigned char *search_path, struct super_block *sb, int xid)
35 {
36         int rc = 0;
37         FILE_UNIX_BASIC_INFO findData;
38         struct cifsTconInfo *pTcon;
39         struct inode *inode;
40         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
41         char *tmp_path;
42
43         pTcon = cifs_sb->tcon;
44         cFYI(1, ("Getting info on %s", search_path));
45         /* could have done a find first instead but this returns more info */
46         rc = CIFSSMBUnixQPathInfo(xid, pTcon, search_path, &findData,
47                                   cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
48                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
49 /*      dump_mem("\nUnixQPathInfo return data", &findData,
50                  sizeof(findData)); */
51         if (rc) {
52                 if (rc == -EREMOTE) {
53                         tmp_path =
54                             kmalloc(strnlen(pTcon->treeName,
55                                             MAX_TREE_SIZE + 1) +
56                                     strnlen(search_path, MAX_PATHCONF) + 1,
57                                     GFP_KERNEL);
58                         if (tmp_path == NULL) {
59                                 return -ENOMEM;
60                         }
61                         /* have to skip first of the double backslash of
62                            UNC name */
63                         strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
64                         strncat(tmp_path, search_path, MAX_PATHCONF);
65                         rc = connect_to_dfs_path(xid, pTcon->ses,
66                                                  /* treename + */ tmp_path,
67                                                  cifs_sb->local_nls, 
68                                                  cifs_sb->mnt_cifs_flags & 
69                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
70                         kfree(tmp_path);
71
72                         /* BB fix up inode etc. */
73                 } else if (rc) {
74                         return rc;
75                 }
76         } else {
77                 struct cifsInodeInfo *cifsInfo;
78                 __u32 type = le32_to_cpu(findData.Type);
79                 __u64 num_of_bytes = le64_to_cpu(findData.NumOfBytes);
80                 __u64 end_of_file = le64_to_cpu(findData.EndOfFile);
81
82                 /* get new inode */
83                 if (*pinode == NULL) {
84                         *pinode = new_inode(sb);
85                         if (*pinode == NULL) 
86                                 return -ENOMEM;
87                         /* Is an i_ino of zero legal? */
88                         /* Are there sanity checks we can use to ensure that
89                            the server is really filling in that field? */
90                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
91                                 (*pinode)->i_ino =
92                                         (unsigned long)findData.UniqueId;
93                         } /* note ino incremented to unique num in new_inode */
94                         insert_inode_hash(*pinode);
95                 }
96
97                 inode = *pinode;
98                 cifsInfo = CIFS_I(inode);
99
100                 cFYI(1, ("Old time %ld", cifsInfo->time));
101                 cifsInfo->time = jiffies;
102                 cFYI(1, ("New time %ld", cifsInfo->time));
103                 /* this is ok to set on every inode revalidate */
104                 atomic_set(&cifsInfo->inUse,1);
105
106                 inode->i_atime =
107                     cifs_NTtimeToUnix(le64_to_cpu(findData.LastAccessTime));
108                 inode->i_mtime =
109                     cifs_NTtimeToUnix(le64_to_cpu
110                                 (findData.LastModificationTime));
111                 inode->i_ctime =
112                     cifs_NTtimeToUnix(le64_to_cpu(findData.LastStatusChange));
113                 inode->i_mode = le64_to_cpu(findData.Permissions);
114                 /* since we set the inode type below we need to mask off
115                    to avoid strange results if bits set above */
116                         inode->i_mode &= ~S_IFMT;
117                 if (type == UNIX_FILE) {
118                         inode->i_mode |= S_IFREG;
119                 } else if (type == UNIX_SYMLINK) {
120                         inode->i_mode |= S_IFLNK;
121                 } else if (type == UNIX_DIR) {
122                         inode->i_mode |= S_IFDIR;
123                 } else if (type == UNIX_CHARDEV) {
124                         inode->i_mode |= S_IFCHR;
125                         inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
126                                 le64_to_cpu(findData.DevMinor) & MINORMASK);
127                 } else if (type == UNIX_BLOCKDEV) {
128                         inode->i_mode |= S_IFBLK;
129                         inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
130                                 le64_to_cpu(findData.DevMinor) & MINORMASK);
131                 } else if (type == UNIX_FIFO) {
132                         inode->i_mode |= S_IFIFO;
133                 } else if (type == UNIX_SOCKET) {
134                         inode->i_mode |= S_IFSOCK;
135                 } else {
136                         /* safest to call it a file if we do not know */
137                         inode->i_mode |= S_IFREG;
138                         cFYI(1,("unknown type %d",type));
139                 }
140                 inode->i_uid = le64_to_cpu(findData.Uid);
141                 inode->i_gid = le64_to_cpu(findData.Gid);
142                 inode->i_nlink = le64_to_cpu(findData.Nlinks);
143
144                 if (is_size_safe_to_change(cifsInfo)) {
145                 /* can not safely change the file size here if the
146                    client is writing to it due to potential races */
147
148                         i_size_write(inode, end_of_file);
149
150                 /* blksize needs to be multiple of two. So safer to default to
151                 blksize and blkbits set in superblock so 2**blkbits and blksize
152                 will match rather than setting to:
153                 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
154
155                 /* This seems incredibly stupid but it turns out that i_blocks
156                    is not related to (i_size / i_blksize), instead 512 byte size
157                    is required for calculating num blocks */
158
159                 /* 512 bytes (2**9) is the fake blocksize that must be used */
160                 /* for this calculation */
161                         inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
162                 }
163
164                 if (num_of_bytes < end_of_file)
165                         cFYI(1, ("allocation size less than end of file"));
166                 cFYI(1, ("Size %ld and blocks %llu",
167                         (unsigned long) inode->i_size,
168                         (unsigned long long)inode->i_blocks));
169                 if (S_ISREG(inode->i_mode)) {
170                         cFYI(1, ("File inode"));
171                         inode->i_op = &cifs_file_inode_ops;
172                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
173                                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
174                                         inode->i_fop = 
175                                                 &cifs_file_direct_nobrl_ops;
176                                 else
177                                         inode->i_fop = &cifs_file_direct_ops;
178                         } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
179                                 inode->i_fop = &cifs_file_nobrl_ops;
180                         else /* not direct, send byte range locks */ 
181                                 inode->i_fop = &cifs_file_ops;
182
183                         /* check if server can support readpages */
184                         if(pTcon->ses->server->maxBuf < 
185                             PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
186                                 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
187                         else
188                                 inode->i_data.a_ops = &cifs_addr_ops;
189                 } else if (S_ISDIR(inode->i_mode)) {
190                         cFYI(1, ("Directory inode"));
191                         inode->i_op = &cifs_dir_inode_ops;
192                         inode->i_fop = &cifs_dir_ops;
193                 } else if (S_ISLNK(inode->i_mode)) {
194                         cFYI(1, ("Symbolic Link inode"));
195                         inode->i_op = &cifs_symlink_inode_ops;
196                 /* tmp_inode->i_fop = */ /* do not need to set to anything */
197                 } else {
198                         cFYI(1, ("Init special inode"));
199                         init_special_inode(inode, inode->i_mode,
200                                            inode->i_rdev);
201                 }
202         }
203         return rc;
204 }
205
206 static int decode_sfu_inode(struct inode * inode, __u64 size,
207                             const unsigned char *path,
208                             struct cifs_sb_info *cifs_sb, int xid)
209 {
210         int rc;
211         int oplock = FALSE;
212         __u16 netfid;
213         struct cifsTconInfo *pTcon = cifs_sb->tcon;
214         char buf[24];
215         unsigned int bytes_read;
216         char * pbuf;
217
218         pbuf = buf;
219
220         if(size == 0) {
221                 inode->i_mode |= S_IFIFO;
222                 return 0;
223         } else if (size < 8) {
224                 return -EINVAL;  /* EOPNOTSUPP? */
225         }
226                 
227         rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
228                          CREATE_NOT_DIR, &netfid, &oplock, NULL,
229                          cifs_sb->local_nls,
230                          cifs_sb->mnt_cifs_flags &
231                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
232         if (rc==0) {
233                 int buf_type = CIFS_NO_BUFFER;
234                         /* Read header */
235                 rc = CIFSSMBRead(xid, pTcon,
236                                  netfid,
237                                  24 /* length */, 0 /* offset */,
238                                  &bytes_read, &pbuf, &buf_type);
239                 if((rc == 0) && (bytes_read >= 8)) {
240                         if(memcmp("IntxBLK", pbuf, 8) == 0) {
241                                 cFYI(1,("Block device"));
242                                 inode->i_mode |= S_IFBLK;
243                                 if(bytes_read == 24) {
244                                         /* we have enough to decode dev num */
245                                         __u64 mjr; /* major */
246                                         __u64 mnr; /* minor */
247                                         mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
248                                         mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
249                                         inode->i_rdev = MKDEV(mjr, mnr);
250                                 }
251                         } else if(memcmp("IntxCHR", pbuf, 8) == 0) {
252                                 cFYI(1,("Char device"));
253                                 inode->i_mode |= S_IFCHR;
254                                 if(bytes_read == 24) {
255                                         /* we have enough to decode dev num */
256                                         __u64 mjr; /* major */
257                                         __u64 mnr; /* minor */
258                                         mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
259                                         mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
260                                         inode->i_rdev = MKDEV(mjr, mnr);
261                                 }
262                         } else if(memcmp("IntxLNK", pbuf, 7) == 0) {
263                                 cFYI(1,("Symlink"));
264                                 inode->i_mode |= S_IFLNK;
265                         } else {
266                                 inode->i_mode |= S_IFREG; /* file? */
267                                 rc = -EOPNOTSUPP; 
268                         }
269                 } else {
270                         inode->i_mode |= S_IFREG; /* then it is a file */
271                         rc = -EOPNOTSUPP; /* or some unknown SFU type */        
272                 }               
273                 CIFSSMBClose(xid, pTcon, netfid);
274         }
275         return rc;
276         
277 }
278
279 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID)  /* SETFILEBITS valid bits */
280
281 static int get_sfu_uid_mode(struct inode * inode,
282                         const unsigned char *path,
283                         struct cifs_sb_info *cifs_sb, int xid)
284 {
285 #ifdef CONFIG_CIFS_XATTR
286         ssize_t rc;
287         char ea_value[4];
288         __u32 mode;
289
290         rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
291                         ea_value, 4 /* size of buf */, cifs_sb->local_nls,
292                         cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
293         if(rc < 0)
294                 return (int)rc;
295         else if (rc > 3) {
296                 mode = le32_to_cpu(*((__le32 *)ea_value));
297                 inode->i_mode &= ~SFBITS_MASK; 
298                 cFYI(1,("special bits 0%o org mode 0%o", mode, inode->i_mode));
299                 inode->i_mode = (mode &  SFBITS_MASK) | inode->i_mode;
300                 cFYI(1,("special mode bits 0%o", mode));
301                 return 0;
302         } else {
303                 return 0;
304         }
305 #else
306         return -EOPNOTSUPP;
307 #endif
308
309                 
310 }
311
312 int cifs_get_inode_info(struct inode **pinode,
313         const unsigned char *search_path, FILE_ALL_INFO *pfindData,
314         struct super_block *sb, int xid)
315 {
316         int rc = 0;
317         struct cifsTconInfo *pTcon;
318         struct inode *inode;
319         struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
320         char *tmp_path;
321         char *buf = NULL;
322
323         pTcon = cifs_sb->tcon;
324         cFYI(1,("Getting info on %s", search_path));
325
326         if ((pfindData == NULL) && (*pinode != NULL)) {
327                 if (CIFS_I(*pinode)->clientCanCacheRead) {
328                         cFYI(1,("No need to revalidate cached inode sizes"));
329                         return rc;
330                 }
331         }
332
333         /* if file info not passed in then get it from server */
334         if (pfindData == NULL) {
335                 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
336                 if (buf == NULL)
337                         return -ENOMEM;
338                 pfindData = (FILE_ALL_INFO *)buf;
339                 /* could do find first instead but this returns more info */
340                 rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
341                               cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
342                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
343                 /* BB optimize code so we do not make the above call
344                 when server claims no NT SMB support and the above call
345                 failed at least once - set flag in tcon or mount */
346                 if((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
347                         rc = SMBQueryInformation(xid, pTcon, search_path,
348                                         pfindData, cifs_sb->local_nls, 
349                                         cifs_sb->mnt_cifs_flags &
350                                           CIFS_MOUNT_MAP_SPECIAL_CHR);
351                 }
352                 
353         }
354         /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
355         if (rc) {
356                 if (rc == -EREMOTE) {
357                         tmp_path =
358                             kmalloc(strnlen
359                                     (pTcon->treeName,
360                                      MAX_TREE_SIZE + 1) +
361                                     strnlen(search_path, MAX_PATHCONF) + 1,
362                                     GFP_KERNEL);
363                         if (tmp_path == NULL) {
364                                 kfree(buf);
365                                 return -ENOMEM;
366                         }
367
368                         strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
369                         strncat(tmp_path, search_path, MAX_PATHCONF);
370                         rc = connect_to_dfs_path(xid, pTcon->ses,
371                                                  /* treename + */ tmp_path,
372                                                  cifs_sb->local_nls, 
373                                                  cifs_sb->mnt_cifs_flags & 
374                                                    CIFS_MOUNT_MAP_SPECIAL_CHR);
375                         kfree(tmp_path);
376                         /* BB fix up inode etc. */
377                 } else if (rc) {
378                         kfree(buf);
379                         return rc;
380                 }
381         } else {
382                 struct cifsInodeInfo *cifsInfo;
383                 __u32 attr = le32_to_cpu(pfindData->Attributes);
384
385                 /* get new inode */
386                 if (*pinode == NULL) {
387                         *pinode = new_inode(sb);
388                         if (*pinode == NULL)
389                                 return -ENOMEM;
390                         /* Is an i_ino of zero legal? Can we use that to check
391                            if the server supports returning inode numbers?  Are
392                            there other sanity checks we can use to ensure that
393                            the server is really filling in that field? */
394
395                         /* We can not use the IndexNumber field by default from
396                            Windows or Samba (in ALL_INFO buf) but we can request
397                            it explicitly.  It may not be unique presumably if
398                            the server has multiple devices mounted under one
399                            share */
400
401                         /* There may be higher info levels that work but are
402                            there Windows server or network appliances for which
403                            IndexNumber field is not guaranteed unique? */
404
405                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM){
406                                 int rc1 = 0;
407                                 __u64 inode_num;
408
409                                 rc1 = CIFSGetSrvInodeNumber(xid, pTcon, 
410                                         search_path, &inode_num, 
411                                         cifs_sb->local_nls,
412                                         cifs_sb->mnt_cifs_flags &
413                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
414                                 if (rc1) {
415                                         cFYI(1,("GetSrvInodeNum rc %d", rc1));
416                                         /* BB EOPNOSUPP disable SERVER_INUM? */
417                                 } else /* do we need cast or hash to ino? */
418                                         (*pinode)->i_ino = inode_num;
419                         } /* else ino incremented to unique num in new_inode*/
420                         insert_inode_hash(*pinode);
421                 }
422                 inode = *pinode;
423                 cifsInfo = CIFS_I(inode);
424                 cifsInfo->cifsAttrs = attr;
425                 cFYI(1, ("Old time %ld", cifsInfo->time));
426                 cifsInfo->time = jiffies;
427                 cFYI(1, ("New time %ld", cifsInfo->time));
428
429                 /* blksize needs to be multiple of two. So safer to default to
430                 blksize and blkbits set in superblock so 2**blkbits and blksize
431                 will match rather than setting to:
432                 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
433
434                 /* Linux can not store file creation time so ignore it */
435                 inode->i_atime =
436                     cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
437                 inode->i_mtime =
438                     cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
439                 inode->i_ctime =
440                     cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
441                 cFYI(0, ("Attributes came in as 0x%x", attr));
442
443                 /* set default mode. will override for dirs below */
444                 if (atomic_read(&cifsInfo->inUse) == 0)
445                         /* new inode, can safely set these fields */
446                         inode->i_mode = cifs_sb->mnt_file_mode;
447                 else /* since we set the inode type below we need to mask off
448                      to avoid strange results if type changes and both get orred in */ 
449                         inode->i_mode &= ~S_IFMT; 
450 /*              if (attr & ATTR_REPARSE)  */
451                 /* We no longer handle these as symlinks because we could not
452                    follow them due to the absolute path with drive letter */
453                 if (attr & ATTR_DIRECTORY) {
454                 /* override default perms since we do not do byte range locking
455                    on dirs */
456                         inode->i_mode = cifs_sb->mnt_dir_mode;
457                         inode->i_mode |= S_IFDIR;
458                 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
459                            (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
460                            /* No need to le64 convert size of zero */
461                            (pfindData->EndOfFile == 0)) {
462                         inode->i_mode = cifs_sb->mnt_file_mode;
463                         inode->i_mode |= S_IFIFO;
464 /* BB Finish for SFU style symlinks and devices */
465                 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
466                            (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
467                         if (decode_sfu_inode(inode, 
468                                          le64_to_cpu(pfindData->EndOfFile),
469                                          search_path,
470                                          cifs_sb, xid)) {
471                                 cFYI(1,("Unrecognized sfu inode type"));
472                         }
473                         cFYI(1,("sfu mode 0%o",inode->i_mode));
474                 } else {
475                         inode->i_mode |= S_IFREG;
476                         /* treat the dos attribute of read-only as read-only
477                            mode e.g. 555 */
478                         if (cifsInfo->cifsAttrs & ATTR_READONLY)
479                                 inode->i_mode &= ~(S_IWUGO);
480                 /* BB add code here -
481                    validate if device or weird share or device type? */
482                 }
483                 if (is_size_safe_to_change(cifsInfo)) {
484                         /* can not safely change the file size here if the
485                            client is writing to it due to potential races */
486                         i_size_write(inode,le64_to_cpu(pfindData->EndOfFile));
487
488                         /* 512 bytes (2**9) is the fake blocksize that must be
489                            used for this calculation */
490                         inode->i_blocks = (512 - 1 + le64_to_cpu(
491                                            pfindData->AllocationSize)) >> 9;
492                 }
493
494                 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
495
496                 /* BB fill in uid and gid here? with help from winbind? 
497                    or retrieve from NTFS stream extended attribute */
498                 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
499                         /* fill in uid, gid, mode from server ACL */
500                         get_sfu_uid_mode(inode, search_path, cifs_sb, xid);
501                 } else if (atomic_read(&cifsInfo->inUse) == 0) {
502                         inode->i_uid = cifs_sb->mnt_uid;
503                         inode->i_gid = cifs_sb->mnt_gid;
504                         /* set so we do not keep refreshing these fields with
505                            bad data after user has changed them in memory */
506                         atomic_set(&cifsInfo->inUse,1);
507                 }
508
509                 if (S_ISREG(inode->i_mode)) {
510                         cFYI(1, ("File inode"));
511                         inode->i_op = &cifs_file_inode_ops;
512                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
513                                 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
514                                         inode->i_fop =
515                                                 &cifs_file_direct_nobrl_ops;
516                                 else
517                                         inode->i_fop = &cifs_file_direct_ops;
518                         } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
519                                 inode->i_fop = &cifs_file_nobrl_ops;
520                         else /* not direct, send byte range locks */
521                                 inode->i_fop = &cifs_file_ops;
522
523                         if(pTcon->ses->server->maxBuf < 
524                              PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
525                                 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
526                         else
527                                 inode->i_data.a_ops = &cifs_addr_ops;
528                 } else if (S_ISDIR(inode->i_mode)) {
529                         cFYI(1, ("Directory inode"));
530                         inode->i_op = &cifs_dir_inode_ops;
531                         inode->i_fop = &cifs_dir_ops;
532                 } else if (S_ISLNK(inode->i_mode)) {
533                         cFYI(1, ("Symbolic Link inode"));
534                         inode->i_op = &cifs_symlink_inode_ops;
535                 } else {
536                         init_special_inode(inode, inode->i_mode,
537                                            inode->i_rdev);
538                 }
539         }
540         kfree(buf);
541         return rc;
542 }
543
544 /* gets root inode */
545 void cifs_read_inode(struct inode *inode)
546 {
547         int xid;
548         struct cifs_sb_info *cifs_sb;
549
550         cifs_sb = CIFS_SB(inode->i_sb);
551         xid = GetXid();
552         if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
553                 cifs_get_inode_info_unix(&inode, "", inode->i_sb,xid);
554         else
555                 cifs_get_inode_info(&inode, "", NULL, inode->i_sb,xid);
556         /* can not call macro FreeXid here since in a void func */
557         _FreeXid(xid);
558 }
559
560 int cifs_unlink(struct inode *inode, struct dentry *direntry)
561 {
562         int rc = 0;
563         int xid;
564         struct cifs_sb_info *cifs_sb;
565         struct cifsTconInfo *pTcon;
566         char *full_path = NULL;
567         struct cifsInodeInfo *cifsInode;
568         FILE_BASIC_INFO *pinfo_buf;
569
570         cFYI(1, ("cifs_unlink, inode = 0x%p", inode));
571
572         xid = GetXid();
573
574         if(inode)
575                 cifs_sb = CIFS_SB(inode->i_sb);
576         else
577                 cifs_sb = CIFS_SB(direntry->d_sb);
578         pTcon = cifs_sb->tcon;
579
580         /* Unlink can be called from rename so we can not grab the sem here
581            since we deadlock otherwise */
582 /*      mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);*/
583         full_path = build_path_from_dentry(direntry);
584 /*      mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);*/
585         if (full_path == NULL) {
586                 FreeXid(xid);
587                 return -ENOMEM;
588         }
589         rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
590                         cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
591
592         if (!rc) {
593                 if (direntry->d_inode)
594                         direntry->d_inode->i_nlink--;
595         } else if (rc == -ENOENT) {
596                 d_drop(direntry);
597         } else if (rc == -ETXTBSY) {
598                 int oplock = FALSE;
599                 __u16 netfid;
600
601                 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
602                                  CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
603                                  &netfid, &oplock, NULL, cifs_sb->local_nls,
604                                  cifs_sb->mnt_cifs_flags & 
605                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
606                 if (rc==0) {
607                         CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
608                                               cifs_sb->local_nls, 
609                                               cifs_sb->mnt_cifs_flags & 
610                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
611                         CIFSSMBClose(xid, pTcon, netfid);
612                         if (direntry->d_inode)
613                                 direntry->d_inode->i_nlink--;
614                 }
615         } else if (rc == -EACCES) {
616                 /* try only if r/o attribute set in local lookup data? */
617                 pinfo_buf = kzalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
618                 if (pinfo_buf) {
619                         /* ATTRS set to normal clears r/o bit */
620                         pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
621                         if (!(pTcon->ses->flags & CIFS_SES_NT4))
622                                 rc = CIFSSMBSetTimes(xid, pTcon, full_path,
623                                                      pinfo_buf,
624                                                      cifs_sb->local_nls,
625                                                      cifs_sb->mnt_cifs_flags & 
626                                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
627                         else
628                                 rc = -EOPNOTSUPP;
629
630                         if (rc == -EOPNOTSUPP) {
631                                 int oplock = FALSE;
632                                 __u16 netfid;
633                         /*      rc = CIFSSMBSetAttrLegacy(xid, pTcon,
634                                                           full_path,
635                                                           (__u16)ATTR_NORMAL,
636                                                           cifs_sb->local_nls); 
637                            For some strange reason it seems that NT4 eats the
638                            old setattr call without actually setting the
639                            attributes so on to the third attempted workaround
640                            */
641
642                         /* BB could scan to see if we already have it open
643                            and pass in pid of opener to function */
644                                 rc = CIFSSMBOpen(xid, pTcon, full_path,
645                                                  FILE_OPEN, SYNCHRONIZE |
646                                                  FILE_WRITE_ATTRIBUTES, 0,
647                                                  &netfid, &oplock, NULL,
648                                                  cifs_sb->local_nls,
649                                                  cifs_sb->mnt_cifs_flags & 
650                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
651                                 if (rc==0) {
652                                         rc = CIFSSMBSetFileTimes(xid, pTcon,
653                                                                  pinfo_buf,
654                                                                  netfid);
655                                         CIFSSMBClose(xid, pTcon, netfid);
656                                 }
657                         }
658                         kfree(pinfo_buf);
659                 }
660                 if (rc==0) {
661                         rc = CIFSSMBDelFile(xid, pTcon, full_path, 
662                                             cifs_sb->local_nls, 
663                                             cifs_sb->mnt_cifs_flags & 
664                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
665                         if (!rc) {
666                                 if (direntry->d_inode)
667                                         direntry->d_inode->i_nlink--;
668                         } else if (rc == -ETXTBSY) {
669                                 int oplock = FALSE;
670                                 __u16 netfid;
671
672                                 rc = CIFSSMBOpen(xid, pTcon, full_path,
673                                                  FILE_OPEN, DELETE,
674                                                  CREATE_NOT_DIR |
675                                                  CREATE_DELETE_ON_CLOSE,
676                                                  &netfid, &oplock, NULL,
677                                                  cifs_sb->local_nls, 
678                                                  cifs_sb->mnt_cifs_flags & 
679                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
680                                 if (rc==0) {
681                                         CIFSSMBRenameOpenFile(xid, pTcon,
682                                                 netfid, NULL,
683                                                 cifs_sb->local_nls,
684                                                 cifs_sb->mnt_cifs_flags &
685                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
686                                         CIFSSMBClose(xid, pTcon, netfid);
687                                         if (direntry->d_inode)
688                                                 direntry->d_inode->i_nlink--;
689                                 }
690                         /* BB if rc = -ETXTBUSY goto the rename logic BB */
691                         }
692                 }
693         }
694         if (direntry->d_inode) {
695                 cifsInode = CIFS_I(direntry->d_inode);
696                 cifsInode->time = 0;    /* will force revalidate to get info
697                                            when needed */
698                 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
699         }
700         if(inode) {
701                 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
702                 cifsInode = CIFS_I(inode);
703                 cifsInode->time = 0;    /* force revalidate of dir as well */
704         }
705
706         kfree(full_path);
707         FreeXid(xid);
708         return rc;
709 }
710
711 int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
712 {
713         int rc = 0;
714         int xid;
715         struct cifs_sb_info *cifs_sb;
716         struct cifsTconInfo *pTcon;
717         char *full_path = NULL;
718         struct inode *newinode = NULL;
719
720         cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
721
722         xid = GetXid();
723
724         cifs_sb = CIFS_SB(inode->i_sb);
725         pTcon = cifs_sb->tcon;
726
727         full_path = build_path_from_dentry(direntry);
728         if (full_path == NULL) {
729                 FreeXid(xid);
730                 return -ENOMEM;
731         }
732         /* BB add setting the equivalent of mode via CreateX w/ACLs */
733         rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
734                           cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
735         if (rc) {
736                 cFYI(1, ("cifs_mkdir returned 0x%x", rc));
737                 d_drop(direntry);
738         } else {
739                 inode->i_nlink++;
740                 if (pTcon->ses->capabilities & CAP_UNIX)
741                         rc = cifs_get_inode_info_unix(&newinode, full_path,
742                                                       inode->i_sb,xid);
743                 else
744                         rc = cifs_get_inode_info(&newinode, full_path, NULL,
745                                                  inode->i_sb,xid);
746
747                 if (pTcon->nocase)
748                         direntry->d_op = &cifs_ci_dentry_ops;
749                 else
750                         direntry->d_op = &cifs_dentry_ops;
751                 d_instantiate(direntry, newinode);
752                 if (direntry->d_inode)
753                         direntry->d_inode->i_nlink = 2;
754                 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
755                         if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
756                                 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
757                                                     mode,
758                                                     (__u64)current->fsuid,
759                                                     (__u64)current->fsgid,
760                                                     0 /* dev_t */,
761                                                     cifs_sb->local_nls,
762                                                     cifs_sb->mnt_cifs_flags &
763                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
764                         } else {
765                                 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
766                                                     mode, (__u64)-1,
767                                                     (__u64)-1, 0 /* dev_t */,
768                                                     cifs_sb->local_nls,
769                                                     cifs_sb->mnt_cifs_flags & 
770                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
771                         }
772                 else {
773                         /* BB to be implemented via Windows secrty descriptors
774                            eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
775                                                  -1, -1, local_nls); */
776                         if(direntry->d_inode) {
777                                 direntry->d_inode->i_mode = mode;
778                                 direntry->d_inode->i_mode |= S_IFDIR;
779                                 if(cifs_sb->mnt_cifs_flags & 
780                                      CIFS_MOUNT_SET_UID) {
781                                         direntry->d_inode->i_uid = 
782                                                 current->fsuid;
783                                         direntry->d_inode->i_gid = 
784                                                 current->fsgid;
785                                 }
786                         }
787                 }
788         }
789         kfree(full_path);
790         FreeXid(xid);
791         return rc;
792 }
793
794 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
795 {
796         int rc = 0;
797         int xid;
798         struct cifs_sb_info *cifs_sb;
799         struct cifsTconInfo *pTcon;
800         char *full_path = NULL;
801         struct cifsInodeInfo *cifsInode;
802
803         cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
804
805         xid = GetXid();
806
807         cifs_sb = CIFS_SB(inode->i_sb);
808         pTcon = cifs_sb->tcon;
809
810         full_path = build_path_from_dentry(direntry);
811         if (full_path == NULL) {
812                 FreeXid(xid);
813                 return -ENOMEM;
814         }
815
816         rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
817                           cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
818
819         if (!rc) {
820                 inode->i_nlink--;
821                 i_size_write(direntry->d_inode,0);
822                 direntry->d_inode->i_nlink = 0;
823         }
824
825         cifsInode = CIFS_I(direntry->d_inode);
826         cifsInode->time = 0;    /* force revalidate to go get info when
827                                    needed */
828         direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
829                 current_fs_time(inode->i_sb);
830
831         kfree(full_path);
832         FreeXid(xid);
833         return rc;
834 }
835
836 int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
837         struct inode *target_inode, struct dentry *target_direntry)
838 {
839         char *fromName;
840         char *toName;
841         struct cifs_sb_info *cifs_sb_source;
842         struct cifs_sb_info *cifs_sb_target;
843         struct cifsTconInfo *pTcon;
844         int xid;
845         int rc = 0;
846
847         xid = GetXid();
848
849         cifs_sb_target = CIFS_SB(target_inode->i_sb);
850         cifs_sb_source = CIFS_SB(source_inode->i_sb);
851         pTcon = cifs_sb_source->tcon;
852
853         if (pTcon != cifs_sb_target->tcon) {
854                 FreeXid(xid);
855                 return -EXDEV;  /* BB actually could be allowed if same server,
856                                    but different share.
857                                    Might eventually add support for this */
858         }
859
860         /* we already  have the rename sem so we do not need to grab it again
861            here to protect the path integrity */
862         fromName = build_path_from_dentry(source_direntry);
863         toName = build_path_from_dentry(target_direntry);
864         if ((fromName == NULL) || (toName == NULL)) {
865                 rc = -ENOMEM;
866                 goto cifs_rename_exit;
867         }
868
869         rc = CIFSSMBRename(xid, pTcon, fromName, toName,
870                            cifs_sb_source->local_nls,
871                            cifs_sb_source->mnt_cifs_flags &
872                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
873         if (rc == -EEXIST) {
874                 /* check if they are the same file because rename of hardlinked
875                    files is a noop */
876                 FILE_UNIX_BASIC_INFO *info_buf_source;
877                 FILE_UNIX_BASIC_INFO *info_buf_target;
878
879                 info_buf_source =
880                         kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
881                 if (info_buf_source != NULL) {
882                         info_buf_target = info_buf_source + 1;
883                         rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
884                                 info_buf_source, cifs_sb_source->local_nls, 
885                                 cifs_sb_source->mnt_cifs_flags &
886                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
887                         if (rc == 0) {
888                                 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
889                                                 info_buf_target,
890                                                 cifs_sb_target->local_nls,
891                                                 /* remap based on source sb */
892                                                 cifs_sb_source->mnt_cifs_flags &
893                                                     CIFS_MOUNT_MAP_SPECIAL_CHR);
894                         }
895                         if ((rc == 0) &&
896                             (info_buf_source->UniqueId ==
897                              info_buf_target->UniqueId)) {
898                         /* do not rename since the files are hardlinked which
899                            is a noop */
900                         } else {
901                         /* we either can not tell the files are hardlinked
902                            (as with Windows servers) or files are not
903                            hardlinked so delete the target manually before
904                            renaming to follow POSIX rather than Windows
905                            semantics */
906                                 cifs_unlink(target_inode, target_direntry);
907                                 rc = CIFSSMBRename(xid, pTcon, fromName,
908                                                    toName,
909                                                    cifs_sb_source->local_nls,
910                                                    cifs_sb_source->mnt_cifs_flags
911                                                    & CIFS_MOUNT_MAP_SPECIAL_CHR);
912                         }
913                         kfree(info_buf_source);
914                 } /* if we can not get memory just leave rc as EEXIST */
915         }
916
917         if (rc) {
918                 cFYI(1, ("rename rc %d", rc));
919         }
920
921         if ((rc == -EIO) || (rc == -EEXIST)) {
922                 int oplock = FALSE;
923                 __u16 netfid;
924
925                 /* BB FIXME Is Generic Read correct for rename? */
926                 /* if renaming directory - we should not say CREATE_NOT_DIR,
927                    need to test renaming open directory, also GENERIC_READ
928                    might not right be right access to request */
929                 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
930                                  CREATE_NOT_DIR, &netfid, &oplock, NULL,
931                                  cifs_sb_source->local_nls, 
932                                  cifs_sb_source->mnt_cifs_flags & 
933                                         CIFS_MOUNT_MAP_SPECIAL_CHR);
934                 if (rc==0) {
935                         CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
936                                               cifs_sb_source->local_nls, 
937                                               cifs_sb_source->mnt_cifs_flags &
938                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
939                         CIFSSMBClose(xid, pTcon, netfid);
940                 }
941         }
942
943 cifs_rename_exit:
944         kfree(fromName);
945         kfree(toName);
946         FreeXid(xid);
947         return rc;
948 }
949
950 int cifs_revalidate(struct dentry *direntry)
951 {
952         int xid;
953         int rc = 0;
954         char *full_path;
955         struct cifs_sb_info *cifs_sb;
956         struct cifsInodeInfo *cifsInode;
957         loff_t local_size;
958         struct timespec local_mtime;
959         int invalidate_inode = FALSE;
960
961         if (direntry->d_inode == NULL)
962                 return -ENOENT;
963
964         cifsInode = CIFS_I(direntry->d_inode);
965
966         if (cifsInode == NULL)
967                 return -ENOENT;
968
969         /* no sense revalidating inode info on file that no one can write */
970         if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
971                 return rc;
972
973         xid = GetXid();
974
975         cifs_sb = CIFS_SB(direntry->d_sb);
976
977         /* can not safely grab the rename sem here if rename calls revalidate
978            since that would deadlock */
979         full_path = build_path_from_dentry(direntry);
980         if (full_path == NULL) {
981                 FreeXid(xid);
982                 return -ENOMEM;
983         }
984         cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
985                  "jiffies %ld", full_path, direntry->d_inode,
986                  direntry->d_inode->i_count.counter, direntry,
987                  direntry->d_time, jiffies));
988
989         if (cifsInode->time == 0) {
990                 /* was set to zero previously to force revalidate */
991         } else if (time_before(jiffies, cifsInode->time + HZ) &&
992                    lookupCacheEnabled) {
993                 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
994                     (direntry->d_inode->i_nlink == 1)) {
995                         kfree(full_path);
996                         FreeXid(xid);
997                         return rc;
998                 } else {
999                         cFYI(1, ("Have to revalidate file due to hardlinks"));
1000                 }
1001         }
1002
1003         /* save mtime and size */
1004         local_mtime = direntry->d_inode->i_mtime;
1005         local_size = direntry->d_inode->i_size;
1006
1007         if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
1008                 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
1009                                               direntry->d_sb,xid);
1010                 if (rc) {
1011                         cFYI(1, ("error on getting revalidate info %d", rc));
1012 /*                      if (rc != -ENOENT)
1013                                 rc = 0; */      /* BB should we cache info on
1014                                                    certain errors? */
1015                 }
1016         } else {
1017                 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
1018                                          direntry->d_sb,xid);
1019                 if (rc) {
1020                         cFYI(1, ("error on getting revalidate info %d", rc));
1021 /*                      if (rc != -ENOENT)
1022                                 rc = 0; */      /* BB should we cache info on
1023                                                    certain errors? */
1024                 }
1025         }
1026         /* should we remap certain errors, access denied?, to zero */
1027
1028         /* if not oplocked, we invalidate inode pages if mtime or file size
1029            had changed on server */
1030
1031         if (timespec_equal(&local_mtime,&direntry->d_inode->i_mtime) && 
1032             (local_size == direntry->d_inode->i_size)) {
1033                 cFYI(1, ("cifs_revalidate - inode unchanged"));
1034         } else {
1035                 /* file may have changed on server */
1036                 if (cifsInode->clientCanCacheRead) {
1037                         /* no need to invalidate inode pages since we were the
1038                            only ones who could have modified the file and the
1039                            server copy is staler than ours */
1040                 } else {
1041                         invalidate_inode = TRUE;
1042                 }
1043         }
1044
1045         /* can not grab this sem since kernel filesys locking documentation
1046            indicates i_mutex may be taken by the kernel on lookup and rename
1047            which could deadlock if we grab the i_mutex here as well */
1048 /*      mutex_lock(&direntry->d_inode->i_mutex);*/
1049         /* need to write out dirty pages here  */
1050         if (direntry->d_inode->i_mapping) {
1051                 /* do we need to lock inode until after invalidate completes
1052                    below? */
1053                 filemap_fdatawrite(direntry->d_inode->i_mapping);
1054         }
1055         if (invalidate_inode) {
1056         /* shrink_dcache not necessary now that cifs dentry ops
1057         are exported for negative dentries */
1058 /*              if(S_ISDIR(direntry->d_inode->i_mode)) 
1059                         shrink_dcache_parent(direntry); */
1060                 if (S_ISREG(direntry->d_inode->i_mode)) {
1061                         if (direntry->d_inode->i_mapping)
1062                                 filemap_fdatawait(direntry->d_inode->i_mapping);
1063                         /* may eventually have to do this for open files too */
1064                         if (list_empty(&(cifsInode->openFileList))) {
1065                                 /* changed on server - flush read ahead pages */
1066                                 cFYI(1, ("Invalidating read ahead data on "
1067                                          "closed file"));
1068                                 invalidate_remote_inode(direntry->d_inode);
1069                         }
1070                 }
1071         }
1072 /*      mutex_unlock(&direntry->d_inode->i_mutex); */
1073         
1074         kfree(full_path);
1075         FreeXid(xid);
1076         return rc;
1077 }
1078
1079 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1080         struct kstat *stat)
1081 {
1082         int err = cifs_revalidate(dentry);
1083         if (!err) {
1084                 generic_fillattr(dentry->d_inode, stat);
1085                 stat->blksize = CIFS_MAX_MSGSIZE;
1086         }
1087         return err;
1088 }
1089
1090 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1091 {
1092         pgoff_t index = from >> PAGE_CACHE_SHIFT;
1093         unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1094         struct page *page;
1095         char *kaddr;
1096         int rc = 0;
1097
1098         page = grab_cache_page(mapping, index);
1099         if (!page)
1100                 return -ENOMEM;
1101
1102         kaddr = kmap_atomic(page, KM_USER0);
1103         memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1104         flush_dcache_page(page);
1105         kunmap_atomic(kaddr, KM_USER0);
1106         unlock_page(page);
1107         page_cache_release(page);
1108         return rc;
1109 }
1110
1111 int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
1112 {
1113         int xid;
1114         struct cifs_sb_info *cifs_sb;
1115         struct cifsTconInfo *pTcon;
1116         char *full_path = NULL;
1117         int rc = -EACCES;
1118         struct cifsFileInfo *open_file = NULL;
1119         FILE_BASIC_INFO time_buf;
1120         int set_time = FALSE;
1121         __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
1122         __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
1123         __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
1124         struct cifsInodeInfo *cifsInode;
1125
1126         xid = GetXid();
1127
1128         cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
1129                  direntry->d_name.name, attrs->ia_valid));
1130
1131         cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
1132         pTcon = cifs_sb->tcon;
1133
1134         if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
1135                 /* check if we have permission to change attrs */
1136                 rc = inode_change_ok(direntry->d_inode, attrs);
1137                 if(rc < 0) {
1138                         FreeXid(xid);
1139                         return rc;
1140                 } else
1141                         rc = 0;
1142         }
1143                 
1144         full_path = build_path_from_dentry(direntry);
1145         if (full_path == NULL) {
1146                 FreeXid(xid);
1147                 return -ENOMEM;
1148         }
1149         cifsInode = CIFS_I(direntry->d_inode);
1150
1151         /* BB check if we need to refresh inode from server now ? BB */
1152
1153         /* need to flush data before changing file size on server */
1154         filemap_write_and_wait(direntry->d_inode->i_mapping);
1155
1156         if (attrs->ia_valid & ATTR_SIZE) {
1157                 /* To avoid spurious oplock breaks from server, in the case of
1158                    inodes that we already have open, avoid doing path based
1159                    setting of file size if we can do it by handle.
1160                    This keeps our caching token (oplock) and avoids timeouts
1161                    when the local oplock break takes longer to flush
1162                    writebehind data than the SMB timeout for the SetPathInfo
1163                    request would allow */
1164
1165                 open_file = find_writable_file(cifsInode);
1166                 if (open_file) {
1167                         __u16 nfid = open_file->netfid;
1168                         __u32 npid = open_file->pid;
1169                         rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
1170                                                 nfid, npid, FALSE);
1171                         atomic_dec(&open_file->wrtPending);
1172                         cFYI(1,("SetFSize for attrs rc = %d", rc));
1173                         if((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1174                                 int bytes_written;
1175                                 rc = CIFSSMBWrite(xid, pTcon,
1176                                                   nfid, 0, attrs->ia_size,
1177                                                   &bytes_written, NULL, NULL,
1178                                                   1 /* 45 seconds */);
1179                                 cFYI(1,("Wrt seteof rc %d", rc));
1180                         }
1181                 } else 
1182                         rc = -EINVAL;
1183
1184                 if (rc != 0) {
1185                         /* Set file size by pathname rather than by handle
1186                            either because no valid, writeable file handle for
1187                            it was found or because there was an error setting
1188                            it by handle */
1189                         rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1190                                            attrs->ia_size, FALSE,
1191                                            cifs_sb->local_nls, 
1192                                            cifs_sb->mnt_cifs_flags &
1193                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1194                         cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1195                         if((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1196                                 __u16 netfid;
1197                                 int oplock = FALSE;
1198
1199                                 rc = SMBLegacyOpen(xid, pTcon, full_path,
1200                                         FILE_OPEN,
1201                                         SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1202                                         CREATE_NOT_DIR, &netfid, &oplock,
1203                                         NULL, cifs_sb->local_nls,
1204                                         cifs_sb->mnt_cifs_flags &
1205                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1206                                 if (rc==0) {
1207                                         int bytes_written;
1208                                         rc = CIFSSMBWrite(xid, pTcon,
1209                                                         netfid, 0,
1210                                                         attrs->ia_size,
1211                                                         &bytes_written, NULL,
1212                                                         NULL, 1 /* 45 sec */);
1213                                         cFYI(1,("wrt seteof rc %d",rc));
1214                                         CIFSSMBClose(xid, pTcon, netfid);
1215                                 }
1216
1217                         }
1218                 }
1219
1220                 /* Server is ok setting allocation size implicitly - no need
1221                    to call:
1222                 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
1223                          cifs_sb->local_nls);
1224                    */
1225
1226                 if (rc == 0) {
1227                         rc = vmtruncate(direntry->d_inode, attrs->ia_size);
1228                         cifs_truncate_page(direntry->d_inode->i_mapping,
1229                                            direntry->d_inode->i_size);
1230                 } else 
1231                         goto cifs_setattr_exit;
1232         }
1233         if (attrs->ia_valid & ATTR_UID) {
1234                 cFYI(1, ("UID changed to %d", attrs->ia_uid));
1235                 uid = attrs->ia_uid;
1236         }
1237         if (attrs->ia_valid & ATTR_GID) {
1238                 cFYI(1, ("GID changed to %d", attrs->ia_gid));
1239                 gid = attrs->ia_gid;
1240         }
1241
1242         time_buf.Attributes = 0;
1243         if (attrs->ia_valid & ATTR_MODE) {
1244                 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
1245                 mode = attrs->ia_mode;
1246         }
1247
1248         if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX)
1249             && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
1250                 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
1251                                          0 /* dev_t */, cifs_sb->local_nls,
1252                                          cifs_sb->mnt_cifs_flags & 
1253                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1254         else if (attrs->ia_valid & ATTR_MODE) {
1255                 rc = 0;
1256                 if ((mode & S_IWUGO) == 0) /* not writeable */ {
1257                         if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0)
1258                                 time_buf.Attributes =
1259                                         cpu_to_le32(cifsInode->cifsAttrs |
1260                                                     ATTR_READONLY);
1261                 } else if ((mode & S_IWUGO) == S_IWUGO) {
1262                         if (cifsInode->cifsAttrs & ATTR_READONLY)
1263                                 time_buf.Attributes =
1264                                         cpu_to_le32(cifsInode->cifsAttrs &
1265                                                     (~ATTR_READONLY));
1266                 }
1267                 /* BB to be implemented -
1268                    via Windows security descriptors or streams */
1269                 /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid,
1270                                       cifs_sb->local_nls); */
1271         }
1272
1273         if (attrs->ia_valid & ATTR_ATIME) {
1274                 set_time = TRUE;
1275                 time_buf.LastAccessTime =
1276                     cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1277         } else
1278                 time_buf.LastAccessTime = 0;
1279
1280         if (attrs->ia_valid & ATTR_MTIME) {
1281                 set_time = TRUE;
1282                 time_buf.LastWriteTime =
1283                     cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1284         } else
1285                 time_buf.LastWriteTime = 0;
1286         /* Do not set ctime explicitly unless other time
1287            stamps are changed explicitly (i.e. by utime()
1288            since we would then have a mix of client and
1289            server times */
1290            
1291         if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1292                 set_time = TRUE;
1293                 /* Although Samba throws this field away
1294                 it may be useful to Windows - but we do
1295                 not want to set ctime unless some other
1296                 timestamp is changing */
1297                 cFYI(1, ("CIFS - CTIME changed"));
1298                 time_buf.ChangeTime =
1299                     cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1300         } else
1301                 time_buf.ChangeTime = 0;
1302
1303         if (set_time || time_buf.Attributes) {
1304                 time_buf.CreationTime = 0;      /* do not change */
1305                 /* In the future we should experiment - try setting timestamps
1306                    via Handle (SetFileInfo) instead of by path */
1307                 if (!(pTcon->ses->flags & CIFS_SES_NT4))
1308                         rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
1309                                              cifs_sb->local_nls,
1310                                              cifs_sb->mnt_cifs_flags &
1311                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1312                 else
1313                         rc = -EOPNOTSUPP;
1314
1315                 if (rc == -EOPNOTSUPP) {
1316                         int oplock = FALSE;
1317                         __u16 netfid;
1318
1319                         cFYI(1, ("calling SetFileInfo since SetPathInfo for "
1320                                  "times not supported by this server"));
1321                         /* BB we could scan to see if we already have it open
1322                            and pass in pid of opener to function */
1323                         rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1324                                          SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1325                                          CREATE_NOT_DIR, &netfid, &oplock,
1326                                          NULL, cifs_sb->local_nls,
1327                                          cifs_sb->mnt_cifs_flags &
1328                                                 CIFS_MOUNT_MAP_SPECIAL_CHR);
1329                         if (rc==0) {
1330                                 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1331                                                          netfid);
1332                                 CIFSSMBClose(xid, pTcon, netfid);
1333                         } else {
1334                         /* BB For even older servers we could convert time_buf
1335                            into old DOS style which uses two second
1336                            granularity */
1337
1338                         /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
1339                                         &time_buf, cifs_sb->local_nls); */
1340                         }
1341                 }
1342                 /* Even if error on time set, no sense failing the call if
1343                 the server would set the time to a reasonable value anyway,
1344                 and this check ensures that we are not being called from
1345                 sys_utimes in which case we ought to fail the call back to
1346                 the user when the server rejects the call */
1347                 if((rc) && (attrs->ia_valid &&
1348                          (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
1349                         rc = 0;
1350         }
1351
1352         /* do not need local check to inode_check_ok since the server does
1353            that */
1354         if (!rc)
1355                 rc = inode_setattr(direntry->d_inode, attrs);
1356 cifs_setattr_exit:
1357         kfree(full_path);
1358         FreeXid(xid);
1359         return rc;
1360 }
1361
1362 void cifs_delete_inode(struct inode *inode)
1363 {
1364         cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
1365         /* may have to add back in if and when safe distributed caching of
1366            directories added e.g. via FindNotify */
1367 }