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