]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/cifs/inode.c
Merge master.kernel.org:/pub/scm/linux/kernel/git/sfrench/cifs-2.6
[net-next-2.6.git] / fs / cifs / inode.c
CommitLineData
1da177e4
LT
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>
1da177e4
LT
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
32int 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;
26a21b98 43 cFYI(1, ("Getting info on %s", search_path));
1da177e4
LT
44 /* could have done a find first instead but this returns more info */
45 rc = CIFSSMBUnixQPathInfo(xid, pTcon, search_path, &findData,
737b758c
SF
46 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
47 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
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,
737b758c
SF
66 cifs_sb->local_nls,
67 cifs_sb->mnt_cifs_flags &
68 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
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);
d0d2f2df 84 if (*pinode == NULL)
1da177e4
LT
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? */
d0d2f2df 89 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
1da177e4
LT
90 (*pinode)->i_ino =
91 (unsigned long)findData.UniqueId;
92 } /* note ino incremented to unique num in new_inode */
1b2b2126
SF
93 if(sb->s_flags & MS_NOATIME)
94 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
95
1da177e4
LT
96 insert_inode_hash(*pinode);
97 }
98
99 inode = *pinode;
100 cifsInfo = CIFS_I(inode);
101
26a21b98 102 cFYI(1, ("Old time %ld", cifsInfo->time));
1da177e4 103 cifsInfo->time = jiffies;
26a21b98 104 cFYI(1, ("New time %ld", cifsInfo->time));
1da177e4
LT
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);
3020a1f5
SF
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;
1da177e4
LT
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;
3020a1f5
SF
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));
1da177e4
LT
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
7ba52631 146 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
1da177e4
LT
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)
8b94bcb9 167 cFYI(1, ("allocation size less than end of file"));
5515eff8
AM
168 cFYI(1, ("Size %ld and blocks %llu",
169 (unsigned long) inode->i_size,
170 (unsigned long long)inode->i_blocks));
1da177e4 171 if (S_ISREG(inode->i_mode)) {
8b94bcb9 172 cFYI(1, ("File inode"));
1da177e4 173 inode->i_op = &cifs_file_inode_ops;
8b94bcb9
SF
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 */
1da177e4 183 inode->i_fop = &cifs_file_ops;
8b94bcb9 184
bfa0d75a
SF
185 /* check if server can support readpages */
186 if(pTcon->ses->server->maxBuf <
273d81d6
DK
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;
1da177e4 191 } else if (S_ISDIR(inode->i_mode)) {
8b94bcb9 192 cFYI(1, ("Directory inode"));
1da177e4
LT
193 inode->i_op = &cifs_dir_inode_ops;
194 inode->i_fop = &cifs_dir_ops;
195 } else if (S_ISLNK(inode->i_mode)) {
8b94bcb9 196 cFYI(1, ("Symbolic Link inode"));
1da177e4
LT
197 inode->i_op = &cifs_symlink_inode_ops;
198 /* tmp_inode->i_fop = */ /* do not need to set to anything */
199 } else {
8b94bcb9 200 cFYI(1, ("Init special inode"));
1da177e4
LT
201 init_special_inode(inode, inode->i_mode,
202 inode->i_rdev);
203 }
204 }
205 return rc;
206}
207
d6e2f2a4
SF
208static 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;
86c96b4b 216 char buf[24];
d6e2f2a4
SF
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) {
ec637e3f 235 int buf_type = CIFS_NO_BUFFER;
d6e2f2a4
SF
236 /* Read header */
237 rc = CIFSSMBRead(xid, pTcon,
238 netfid,
86c96b4b 239 24 /* length */, 0 /* offset */,
ec637e3f 240 &bytes_read, &pbuf, &buf_type);
86c96b4b 241 if((rc == 0) && (bytes_read >= 8)) {
9e294f1c
SF
242 if(memcmp("IntxBLK", pbuf, 8) == 0) {
243 cFYI(1,("Block device"));
3020a1f5 244 inode->i_mode |= S_IFBLK;
86c96b4b
SF
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 }
9e294f1c
SF
253 } else if(memcmp("IntxCHR", pbuf, 8) == 0) {
254 cFYI(1,("Char device"));
3020a1f5 255 inode->i_mode |= S_IFCHR;
86c96b4b
SF
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 }
9e294f1c
SF
264 } else if(memcmp("IntxLNK", pbuf, 7) == 0) {
265 cFYI(1,("Symlink"));
3020a1f5 266 inode->i_mode |= S_IFLNK;
86c96b4b
SF
267 } else {
268 inode->i_mode |= S_IFREG; /* file? */
269 rc = -EOPNOTSUPP;
270 }
3020a1f5
SF
271 } else {
272 inode->i_mode |= S_IFREG; /* then it is a file */
273 rc = -EOPNOTSUPP; /* or some unknown SFU type */
ec637e3f 274 }
d6e2f2a4 275 CIFSSMBClose(xid, pTcon, netfid);
d6e2f2a4
SF
276 }
277 return rc;
278
279}
280
9e294f1c
SF
281#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
282
283static int get_sfu_uid_mode(struct inode * inode,
284 const unsigned char *path,
285 struct cifs_sb_info *cifs_sb, int xid)
286{
3020a1f5 287#ifdef CONFIG_CIFS_XATTR
9e294f1c
SF
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));
c119b87d 299 inode->i_mode &= ~SFBITS_MASK;
3020a1f5 300 cFYI(1,("special bits 0%o org mode 0%o", mode, inode->i_mode));
9e294f1c
SF
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 }
3020a1f5
SF
307#else
308 return -EOPNOTSUPP;
309#endif
310
9e294f1c
SF
311
312}
313
1da177e4
LT
314int 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;
8d6286fd 324 int adjustTZ = FALSE;
1da177e4
LT
325
326 pTcon = cifs_sb->tcon;
d6e2f2a4 327 cFYI(1,("Getting info on %s", search_path));
1da177e4 328
d0d2f2df
SF
329 if ((pfindData == NULL) && (*pinode != NULL)) {
330 if (CIFS_I(*pinode)->clientCanCacheRead) {
1da177e4
LT
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 */
d0d2f2df 337 if (pfindData == NULL) {
1da177e4 338 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
d0d2f2df 339 if (buf == NULL)
1da177e4
LT
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,
acf1a1b1 344 0 /* not legacy */,
6b8edfe0 345 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
737b758c 346 CIFS_MOUNT_MAP_SPECIAL_CHR);
6b8edfe0
SF
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);
8d6286fd 355 adjustTZ = TRUE;
6b8edfe0
SF
356 }
357
1da177e4
LT
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,
737b758c
SF
377 cifs_sb->local_nls,
378 cifs_sb->mnt_cifs_flags &
379 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
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);
acf1a1b1
SF
393 if (*pinode == NULL) {
394 kfree(buf);
1da177e4 395 return -ENOMEM;
acf1a1b1 396 }
1da177e4
LT
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
d0d2f2df 412 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM){
1da177e4
LT
413 int rc1 = 0;
414 __u64 inode_num;
415
416 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
417 search_path, &inode_num,
737b758c
SF
418 cifs_sb->local_nls,
419 cifs_sb->mnt_cifs_flags &
420 CIFS_MOUNT_MAP_SPECIAL_CHR);
d0d2f2df 421 if (rc1) {
1da177e4
LT
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*/
1b2b2126
SF
427 if(sb->s_flags & MS_NOATIME)
428 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
1da177e4
LT
429 insert_inode_hash(*pinode);
430 }
431 inode = *pinode;
432 cifsInfo = CIFS_I(inode);
433 cifsInfo->cifsAttrs = attr;
26a21b98 434 cFYI(1, ("Old time %ld", cifsInfo->time));
1da177e4 435 cifsInfo->time = jiffies;
26a21b98 436 cFYI(1, ("New time %ld", cifsInfo->time));
1da177e4
LT
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
26a21b98 443 /* Linux can not store file creation time so ignore it */
1bd5bbcb
SF
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;
1da177e4
LT
449 inode->i_mtime =
450 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
451 inode->i_ctime =
452 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
26a21b98 453 cFYI(0, ("Attributes came in as 0x%x", attr));
8d6286fd
SF
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 }
1da177e4
LT
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;
3020a1f5
SF
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;
1da177e4
LT
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;
eda3c029
SF
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;
d6e2f2a4
SF
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 }
3020a1f5 489 cFYI(1,("sfu mode 0%o",inode->i_mode));
1da177e4
LT
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 }
7ba52631
SF
499 if (is_size_safe_to_change(cifsInfo, le64_to_cpu(pfindData->EndOfFile))) {
500 /* can not safely shrink the file size here if the
1da177e4
LT
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 */
9e294f1c
SF
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) {
1da177e4
LT
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)) {
d6e2f2a4 526 cFYI(1, ("File inode"));
1da177e4 527 inode->i_op = &cifs_file_inode_ops;
8b94bcb9
SF
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 */
1da177e4 537 inode->i_fop = &cifs_file_ops;
8b94bcb9 538
bfa0d75a 539 if(pTcon->ses->server->maxBuf <
273d81d6
DK
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;
1da177e4 544 } else if (S_ISDIR(inode->i_mode)) {
d6e2f2a4 545 cFYI(1, ("Directory inode"));
1da177e4
LT
546 inode->i_op = &cifs_dir_inode_ops;
547 inode->i_fop = &cifs_dir_ops;
548 } else if (S_ISLNK(inode->i_mode)) {
d6e2f2a4 549 cFYI(1, ("Symbolic Link inode"));
1da177e4
LT
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 */
561void 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
576int 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
06bcfedd 586 cFYI(1, ("cifs_unlink, inode = 0x%p", inode));
1da177e4
LT
587
588 xid = GetXid();
589
6910ab30
SF
590 if(inode)
591 cifs_sb = CIFS_SB(inode->i_sb);
592 else
06bcfedd 593 cifs_sb = CIFS_SB(direntry->d_sb);
1da177e4
LT
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 */
a11f3a05 598/* mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);*/
7f57356b 599 full_path = build_path_from_dentry(direntry);
a11f3a05 600/* mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);*/
1da177e4
LT
601 if (full_path == NULL) {
602 FreeXid(xid);
603 return -ENOMEM;
604 }
737b758c
SF
605 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
606 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
607
608 if (!rc) {
d0d2f2df 609 if (direntry->d_inode)
9a53c3a7 610 drop_nlink(direntry->d_inode);
1da177e4
LT
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,
737b758c
SF
619 &netfid, &oplock, NULL, cifs_sb->local_nls,
620 cifs_sb->mnt_cifs_flags &
621 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
622 if (rc==0) {
623 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
737b758c
SF
624 cifs_sb->local_nls,
625 cifs_sb->mnt_cifs_flags &
626 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4 627 CIFSSMBClose(xid, pTcon, netfid);
d0d2f2df 628 if (direntry->d_inode)
9a53c3a7 629 drop_nlink(direntry->d_inode);
1da177e4
LT
630 }
631 } else if (rc == -EACCES) {
632 /* try only if r/o attribute set in local lookup data? */
a048d7a8 633 pinfo_buf = kzalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
1da177e4 634 if (pinfo_buf) {
1da177e4
LT
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,
737b758c
SF
640 cifs_sb->local_nls,
641 cifs_sb->mnt_cifs_flags &
642 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
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,
737b758c
SF
664 cifs_sb->local_nls,
665 cifs_sb->mnt_cifs_flags &
666 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
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) {
737b758c
SF
677 rc = CIFSSMBDelFile(xid, pTcon, full_path,
678 cifs_sb->local_nls,
679 cifs_sb->mnt_cifs_flags &
680 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4 681 if (!rc) {
d0d2f2df 682 if (direntry->d_inode)
9a53c3a7 683 drop_nlink(direntry->d_inode);
1da177e4
LT
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,
737b758c
SF
693 cifs_sb->local_nls,
694 cifs_sb->mnt_cifs_flags &
695 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
696 if (rc==0) {
697 CIFSSMBRenameOpenFile(xid, pTcon,
698 netfid, NULL,
737b758c
SF
699 cifs_sb->local_nls,
700 cifs_sb->mnt_cifs_flags &
701 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4 702 CIFSSMBClose(xid, pTcon, netfid);
d0d2f2df 703 if (direntry->d_inode)
9a53c3a7 704 drop_nlink(direntry->d_inode);
1da177e4
LT
705 }
706 /* BB if rc = -ETXTBUSY goto the rename logic BB */
707 }
708 }
709 }
d0d2f2df 710 if (direntry->d_inode) {
b2aeb9d5
SF
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 }
06bcfedd
SF
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 }
1da177e4
LT
721
722 kfree(full_path);
723 FreeXid(xid);
724 return rc;
725}
726
727int 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
6473a559 736 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
1da177e4
LT
737
738 xid = GetXid();
739
740 cifs_sb = CIFS_SB(inode->i_sb);
741 pTcon = cifs_sb->tcon;
742
7f57356b 743 full_path = build_path_from_dentry(direntry);
1da177e4
LT
744 if (full_path == NULL) {
745 FreeXid(xid);
746 return -ENOMEM;
747 }
748 /* BB add setting the equivalent of mode via CreateX w/ACLs */
737b758c
SF
749 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
750 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4 751 if (rc) {
26a21b98 752 cFYI(1, ("cifs_mkdir returned 0x%x", rc));
1da177e4
LT
753 d_drop(direntry);
754 } else {
d8c76e6f 755 inc_nlink(inode);
1da177e4
LT
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
b92327fe
SF
763 if (pTcon->nocase)
764 direntry->d_op = &cifs_ci_dentry_ops;
765 else
766 direntry->d_op = &cifs_dentry_ops;
1da177e4
LT
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)
d0d2f2df 771 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1da177e4
LT
772 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
773 mode,
83451879
SF
774 (__u64)current->fsuid,
775 (__u64)current->fsgid,
1da177e4 776 0 /* dev_t */,
737b758c
SF
777 cifs_sb->local_nls,
778 cifs_sb->mnt_cifs_flags &
779 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
780 } else {
781 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
782 mode, (__u64)-1,
783 (__u64)-1, 0 /* dev_t */,
737b758c
SF
784 cifs_sb->local_nls,
785 cifs_sb->mnt_cifs_flags &
786 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
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); */
6473a559
SF
792 if(direntry->d_inode) {
793 direntry->d_inode->i_mode = mode;
25741b3e 794 direntry->d_inode->i_mode |= S_IFDIR;
6473a559
SF
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 }
2a138ebb 803 }
1da177e4
LT
804 }
805 kfree(full_path);
806 FreeXid(xid);
807 return rc;
808}
809
810int 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
26a21b98 819 cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
1da177e4
LT
820
821 xid = GetXid();
822
823 cifs_sb = CIFS_SB(inode->i_sb);
824 pTcon = cifs_sb->tcon;
825
7f57356b 826 full_path = build_path_from_dentry(direntry);
1da177e4
LT
827 if (full_path == NULL) {
828 FreeXid(xid);
829 return -ENOMEM;
830 }
831
737b758c
SF
832 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
833 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
834
835 if (!rc) {
9a53c3a7 836 drop_nlink(inode);
1da177e4 837 i_size_write(direntry->d_inode,0);
ce71ec36 838 clear_nlink(direntry->d_inode);
1da177e4
LT
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
852int 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 */
7f57356b
SF
878 fromName = build_path_from_dentry(source_direntry);
879 toName = build_path_from_dentry(target_direntry);
1da177e4
LT
880 if ((fromName == NULL) || (toName == NULL)) {
881 rc = -ENOMEM;
882 goto cifs_rename_exit;
883 }
884
885 rc = CIFSSMBRename(xid, pTcon, fromName, toName,
737b758c
SF
886 cifs_sb_source->local_nls,
887 cifs_sb_source->mnt_cifs_flags &
888 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
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;
8e87d4dc
SF
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 */
1da177e4
LT
907 if (rc == 0) {
908 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
909 info_buf_target,
737b758c
SF
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);
1da177e4
LT
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,
737b758c
SF
929 cifs_sb_source->local_nls,
930 cifs_sb_source->mnt_cifs_flags
931 & CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
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,
737b758c
SF
951 cifs_sb_source->local_nls,
952 cifs_sb_source->mnt_cifs_flags &
953 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4 954 if (rc==0) {
8e87d4dc 955 rc = CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
737b758c
SF
956 cifs_sb_source->local_nls,
957 cifs_sb_source->mnt_cifs_flags &
958 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
959 CIFSSMBClose(xid, pTcon, netfid);
960 }
961 }
962
963cifs_rename_exit:
964 kfree(fromName);
965 kfree(toName);
966 FreeXid(xid);
967 return rc;
968}
969
970int 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 */
7f57356b 999 full_path = build_path_from_dentry(direntry);
1da177e4
LT
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
1b1dcc1b
JS
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);*/
1da177e4
LT
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) {
3abb9272
SF
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 }
1da177e4
LT
1090 }
1091 }
1b1dcc1b 1092/* mutex_unlock(&direntry->d_inode->i_mutex); */
1da177e4
LT
1093
1094 kfree(full_path);
1095 FreeXid(xid);
1096 return rc;
1097}
1098
1099int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1100 struct kstat *stat)
1101{
1102 int err = cifs_revalidate(dentry);
5fe14c85 1103 if (!err) {
1da177e4 1104 generic_fillattr(dentry->d_inode, stat);
5fe14c85
SF
1105 stat->blksize = CIFS_MAX_MSGSIZE;
1106 }
1da177e4
LT
1107 return err;
1108}
1109
1110static 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
1131int 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;
1da177e4
LT
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;
1da177e4
LT
1145
1146 xid = GetXid();
1147
3979877e 1148 cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
1da177e4 1149 direntry->d_name.name, attrs->ia_valid));
6473a559 1150
1da177e4
LT
1151 cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
1152 pTcon = cifs_sb->tcon;
1153
2a138ebb 1154 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
6473a559
SF
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
7f57356b 1164 full_path = build_path_from_dentry(direntry);
1da177e4
LT
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 */
28fd1298 1174 filemap_write_and_wait(direntry->d_inode->i_mapping);
1da177e4
LT
1175
1176 if (attrs->ia_valid & ATTR_SIZE) {
1da177e4
LT
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 */
3979877e 1184
6148a742
SF
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);
23e7dd7d 1191 atomic_dec(&open_file->wrtPending);
6148a742 1192 cFYI(1,("SetFSize for attrs rc = %d", rc));
083d3a2c 1193 if((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
6148a742
SF
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));
1da177e4 1200 }
6473a559
SF
1201 } else
1202 rc = -EINVAL;
1203
1da177e4
LT
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,
737b758c
SF
1211 cifs_sb->local_nls,
1212 cifs_sb->mnt_cifs_flags &
1213 CIFS_MOUNT_MAP_SPECIAL_CHR);
e30dcf3a 1214 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
083d3a2c 1215 if((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
e30dcf3a
SF
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 }
1da177e4
LT
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);
e30dcf3a
SF
1250 } else
1251 goto cifs_setattr_exit;
1da177e4
LT
1252 }
1253 if (attrs->ia_valid & ATTR_UID) {
e30dcf3a 1254 cFYI(1, ("UID changed to %d", attrs->ia_uid));
1da177e4 1255 uid = attrs->ia_uid;
1da177e4
LT
1256 }
1257 if (attrs->ia_valid & ATTR_GID) {
e30dcf3a 1258 cFYI(1, ("GID changed to %d", attrs->ia_gid));
1da177e4 1259 gid = attrs->ia_gid;
1da177e4
LT
1260 }
1261
1262 time_buf.Attributes = 0;
1263 if (attrs->ia_valid & ATTR_MODE) {
e30dcf3a 1264 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
1da177e4 1265 mode = attrs->ia_mode;
1da177e4
LT
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,
737b758c
SF
1271 0 /* dev_t */, cifs_sb->local_nls,
1272 cifs_sb->mnt_cifs_flags &
1273 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4 1274 else if (attrs->ia_valid & ATTR_MODE) {
cdbce9c8 1275 rc = 0;
1da177e4
LT
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;
e30dcf3a
SF
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)) {
1da177e4 1312 set_time = TRUE;
e30dcf3a
SF
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 */
26a21b98 1317 cFYI(1, ("CIFS - CTIME changed"));
1da177e4
LT
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) {
1da177e4
LT
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,
737b758c
SF
1329 cifs_sb->local_nls,
1330 cifs_sb->mnt_cifs_flags &
1331 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
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,
737b758c
SF
1346 NULL, cifs_sb->local_nls,
1347 cifs_sb->mnt_cifs_flags &
1348 CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
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 }
e30dcf3a
SF
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 */
c14e894b 1367 if((rc) && (attrs->ia_valid &
e30dcf3a
SF
1368 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
1369 rc = 0;
1da177e4
LT
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);
e30dcf3a 1376cifs_setattr_exit:
1da177e4
LT
1377 kfree(full_path);
1378 FreeXid(xid);
1379 return rc;
1380}
1381
1382void cifs_delete_inode(struct inode *inode)
1383{
26a21b98 1384 cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
1da177e4
LT
1385 /* may have to add back in if and when safe distributed caching of
1386 directories added e.g. via FindNotify */
1387}