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