blob: 0485c6d6ecd525b0080021898a27aaf551f44cf4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/inode.c
3 *
4 * Copyright (C) International Business Machines Corp., 2002,2005
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21#include <linux/fs.h>
22#include <linux/buffer_head.h>
23#include <linux/stat.h>
24#include <linux/pagemap.h>
25#include <asm/div64.h>
26#include "cifsfs.h"
27#include "cifspdu.h"
28#include "cifsglob.h"
29#include "cifsproto.h"
30#include "cifs_debug.h"
31#include "cifs_fs_sb.h"
32
33int cifs_get_inode_info_unix(struct inode **pinode,
34 const unsigned char *search_path, struct super_block *sb, int xid)
35{
36 int rc = 0;
37 FILE_UNIX_BASIC_INFO findData;
38 struct cifsTconInfo *pTcon;
39 struct inode *inode;
40 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
41 char *tmp_path;
42
43 pTcon = cifs_sb->tcon;
44 cFYI(1, (" Getting info on %s ", search_path));
45 /* could have done a find first instead but this returns more info */
46 rc = CIFSSMBUnixQPathInfo(xid, pTcon, search_path, &findData,
Steve French737b7582005-04-28 22:41:06 -070047 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
48 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* dump_mem("\nUnixQPathInfo return data", &findData,
50 sizeof(findData)); */
51 if (rc) {
52 if (rc == -EREMOTE) {
53 tmp_path =
54 kmalloc(strnlen(pTcon->treeName,
55 MAX_TREE_SIZE + 1) +
56 strnlen(search_path, MAX_PATHCONF) + 1,
57 GFP_KERNEL);
58 if (tmp_path == NULL) {
59 return -ENOMEM;
60 }
61 /* have to skip first of the double backslash of
62 UNC name */
63 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
64 strncat(tmp_path, search_path, MAX_PATHCONF);
65 rc = connect_to_dfs_path(xid, pTcon->ses,
66 /* treename + */ tmp_path,
Steve French737b7582005-04-28 22:41:06 -070067 cifs_sb->local_nls,
68 cifs_sb->mnt_cifs_flags &
69 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 kfree(tmp_path);
71
72 /* BB fix up inode etc. */
73 } else if (rc) {
74 return rc;
75 }
76 } else {
77 struct cifsInodeInfo *cifsInfo;
78 __u32 type = le32_to_cpu(findData.Type);
79 __u64 num_of_bytes = le64_to_cpu(findData.NumOfBytes);
80 __u64 end_of_file = le64_to_cpu(findData.EndOfFile);
81
82 /* get new inode */
83 if (*pinode == NULL) {
84 *pinode = new_inode(sb);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -070085 if (*pinode == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return -ENOMEM;
87 /* Is an i_ino of zero legal? */
88 /* Are there sanity checks we can use to ensure that
89 the server is really filling in that field? */
Steve Frenchd0d2f2d2005-06-02 15:12:36 -070090 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 (*pinode)->i_ino =
92 (unsigned long)findData.UniqueId;
93 } /* note ino incremented to unique num in new_inode */
94 insert_inode_hash(*pinode);
95 }
96
97 inode = *pinode;
98 cifsInfo = CIFS_I(inode);
99
100 cFYI(1, (" Old time %ld ", cifsInfo->time));
101 cifsInfo->time = jiffies;
102 cFYI(1, (" New time %ld ", cifsInfo->time));
103 /* this is ok to set on every inode revalidate */
104 atomic_set(&cifsInfo->inUse,1);
105
106 inode->i_atime =
107 cifs_NTtimeToUnix(le64_to_cpu(findData.LastAccessTime));
108 inode->i_mtime =
109 cifs_NTtimeToUnix(le64_to_cpu
110 (findData.LastModificationTime));
111 inode->i_ctime =
112 cifs_NTtimeToUnix(le64_to_cpu(findData.LastStatusChange));
113 inode->i_mode = le64_to_cpu(findData.Permissions);
114 if (type == UNIX_FILE) {
115 inode->i_mode |= S_IFREG;
116 } else if (type == UNIX_SYMLINK) {
117 inode->i_mode |= S_IFLNK;
118 } else if (type == UNIX_DIR) {
119 inode->i_mode |= S_IFDIR;
120 } else if (type == UNIX_CHARDEV) {
121 inode->i_mode |= S_IFCHR;
122 inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
123 le64_to_cpu(findData.DevMinor) & MINORMASK);
124 } else if (type == UNIX_BLOCKDEV) {
125 inode->i_mode |= S_IFBLK;
126 inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
127 le64_to_cpu(findData.DevMinor) & MINORMASK);
128 } else if (type == UNIX_FIFO) {
129 inode->i_mode |= S_IFIFO;
130 } else if (type == UNIX_SOCKET) {
131 inode->i_mode |= S_IFSOCK;
132 }
133 inode->i_uid = le64_to_cpu(findData.Uid);
134 inode->i_gid = le64_to_cpu(findData.Gid);
135 inode->i_nlink = le64_to_cpu(findData.Nlinks);
136
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700137 if (is_size_safe_to_change(cifsInfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /* can not safely change the file size here if the
139 client is writing to it due to potential races */
140
141 i_size_write(inode, end_of_file);
142
143 /* blksize needs to be multiple of two. So safer to default to
144 blksize and blkbits set in superblock so 2**blkbits and blksize
145 will match rather than setting to:
146 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
147
148 /* This seems incredibly stupid but it turns out that i_blocks
149 is not related to (i_size / i_blksize), instead 512 byte size
150 is required for calculating num blocks */
151
152 /* 512 bytes (2**9) is the fake blocksize that must be used */
153 /* for this calculation */
154 inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
155 }
156
157 if (num_of_bytes < end_of_file)
158 cFYI(1, ("allocation size less than end of file "));
159 cFYI(1,
160 ("Size %ld and blocks %ld",
161 (unsigned long) inode->i_size, inode->i_blocks));
162 if (S_ISREG(inode->i_mode)) {
163 cFYI(1, (" File inode "));
164 inode->i_op = &cifs_file_inode_ops;
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700165 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 inode->i_fop = &cifs_file_direct_ops;
167 else
168 inode->i_fop = &cifs_file_ops;
Steve Frenchc46fa8a2005-08-18 20:49:57 -0700169 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
170 inode->i_fop->lock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 inode->i_data.a_ops = &cifs_addr_ops;
172 } else if (S_ISDIR(inode->i_mode)) {
173 cFYI(1, (" Directory inode"));
174 inode->i_op = &cifs_dir_inode_ops;
175 inode->i_fop = &cifs_dir_ops;
176 } else if (S_ISLNK(inode->i_mode)) {
177 cFYI(1, (" Symbolic Link inode "));
178 inode->i_op = &cifs_symlink_inode_ops;
179 /* tmp_inode->i_fop = */ /* do not need to set to anything */
180 } else {
181 cFYI(1, (" Init special inode "));
182 init_special_inode(inode, inode->i_mode,
183 inode->i_rdev);
184 }
185 }
186 return rc;
187}
188
189int cifs_get_inode_info(struct inode **pinode,
190 const unsigned char *search_path, FILE_ALL_INFO *pfindData,
191 struct super_block *sb, int xid)
192{
193 int rc = 0;
194 struct cifsTconInfo *pTcon;
195 struct inode *inode;
196 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
197 char *tmp_path;
198 char *buf = NULL;
199
200 pTcon = cifs_sb->tcon;
201 cFYI(1,("Getting info on %s ", search_path));
202
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700203 if ((pfindData == NULL) && (*pinode != NULL)) {
204 if (CIFS_I(*pinode)->clientCanCacheRead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 cFYI(1,("No need to revalidate cached inode sizes"));
206 return rc;
207 }
208 }
209
210 /* if file info not passed in then get it from server */
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700211 if (pfindData == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700213 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return -ENOMEM;
215 pfindData = (FILE_ALL_INFO *)buf;
216 /* could do find first instead but this returns more info */
217 rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
Steve French6b8edfe2005-08-23 20:26:03 -0700218 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700219 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French6b8edfe2005-08-23 20:26:03 -0700220 /* BB optimize code so we do not make the above call
221 when server claims no NT SMB support and the above call
222 failed at least once - set flag in tcon or mount */
223 if((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
224 rc = SMBQueryInformation(xid, pTcon, search_path,
225 pfindData, cifs_sb->local_nls,
226 cifs_sb->mnt_cifs_flags &
227 CIFS_MOUNT_MAP_SPECIAL_CHR);
228 }
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
232 if (rc) {
233 if (rc == -EREMOTE) {
234 tmp_path =
235 kmalloc(strnlen
236 (pTcon->treeName,
237 MAX_TREE_SIZE + 1) +
238 strnlen(search_path, MAX_PATHCONF) + 1,
239 GFP_KERNEL);
240 if (tmp_path == NULL) {
241 kfree(buf);
242 return -ENOMEM;
243 }
244
245 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
246 strncat(tmp_path, search_path, MAX_PATHCONF);
247 rc = connect_to_dfs_path(xid, pTcon->ses,
248 /* treename + */ tmp_path,
Steve French737b7582005-04-28 22:41:06 -0700249 cifs_sb->local_nls,
250 cifs_sb->mnt_cifs_flags &
251 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 kfree(tmp_path);
253 /* BB fix up inode etc. */
254 } else if (rc) {
255 kfree(buf);
256 return rc;
257 }
258 } else {
259 struct cifsInodeInfo *cifsInfo;
260 __u32 attr = le32_to_cpu(pfindData->Attributes);
261
262 /* get new inode */
263 if (*pinode == NULL) {
264 *pinode = new_inode(sb);
265 if (*pinode == NULL)
266 return -ENOMEM;
267 /* Is an i_ino of zero legal? Can we use that to check
268 if the server supports returning inode numbers? Are
269 there other sanity checks we can use to ensure that
270 the server is really filling in that field? */
271
272 /* We can not use the IndexNumber field by default from
273 Windows or Samba (in ALL_INFO buf) but we can request
274 it explicitly. It may not be unique presumably if
275 the server has multiple devices mounted under one
276 share */
277
278 /* There may be higher info levels that work but are
279 there Windows server or network appliances for which
280 IndexNumber field is not guaranteed unique? */
281
282#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700283 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 int rc1 = 0;
285 __u64 inode_num;
286
287 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
288 search_path, &inode_num,
Steve French737b7582005-04-28 22:41:06 -0700289 cifs_sb->local_nls,
290 cifs_sb->mnt_cifs_flags &
291 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700292 if (rc1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 cFYI(1,("GetSrvInodeNum rc %d", rc1));
294 /* BB EOPNOSUPP disable SERVER_INUM? */
295 } else /* do we need cast or hash to ino? */
296 (*pinode)->i_ino = inode_num;
297 } /* else ino incremented to unique num in new_inode*/
298#endif /* CIFS_EXPERIMENTAL */
299 insert_inode_hash(*pinode);
300 }
301 inode = *pinode;
302 cifsInfo = CIFS_I(inode);
303 cifsInfo->cifsAttrs = attr;
304 cFYI(1, (" Old time %ld ", cifsInfo->time));
305 cifsInfo->time = jiffies;
306 cFYI(1, (" New time %ld ", cifsInfo->time));
307
308 /* blksize needs to be multiple of two. So safer to default to
309 blksize and blkbits set in superblock so 2**blkbits and blksize
310 will match rather than setting to:
311 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
312
313 /* Linux can not store file creation time unfortunately so we ignore it */
314 inode->i_atime =
315 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
316 inode->i_mtime =
317 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
318 inode->i_ctime =
319 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
320 cFYI(0, (" Attributes came in as 0x%x ", attr));
321
322 /* set default mode. will override for dirs below */
323 if (atomic_read(&cifsInfo->inUse) == 0)
324 /* new inode, can safely set these fields */
325 inode->i_mode = cifs_sb->mnt_file_mode;
326
327/* if (attr & ATTR_REPARSE) */
328 /* We no longer handle these as symlinks because we could not
329 follow them due to the absolute path with drive letter */
330 if (attr & ATTR_DIRECTORY) {
331 /* override default perms since we do not do byte range locking
332 on dirs */
333 inode->i_mode = cifs_sb->mnt_dir_mode;
334 inode->i_mode |= S_IFDIR;
Steve Frencheda3c0292005-07-21 15:20:28 -0700335 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
336 (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
337 /* No need to le64 convert size of zero */
338 (pfindData->EndOfFile == 0)) {
339 inode->i_mode = cifs_sb->mnt_file_mode;
340 inode->i_mode |= S_IFIFO;
341/* BB Finish for SFU style symlinks and devies */
342/* } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
343 (cifsInfo->cifsAttrs & ATTR_SYSTEM) && ) */
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 } else {
346 inode->i_mode |= S_IFREG;
347 /* treat the dos attribute of read-only as read-only
348 mode e.g. 555 */
349 if (cifsInfo->cifsAttrs & ATTR_READONLY)
350 inode->i_mode &= ~(S_IWUGO);
351 /* BB add code here -
352 validate if device or weird share or device type? */
353 }
354 if (is_size_safe_to_change(cifsInfo)) {
355 /* can not safely change the file size here if the
356 client is writing to it due to potential races */
357 i_size_write(inode,le64_to_cpu(pfindData->EndOfFile));
358
359 /* 512 bytes (2**9) is the fake blocksize that must be
360 used for this calculation */
361 inode->i_blocks = (512 - 1 + le64_to_cpu(
362 pfindData->AllocationSize)) >> 9;
363 }
364
365 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
366
367 /* BB fill in uid and gid here? with help from winbind?
368 or retrieve from NTFS stream extended attribute */
369 if (atomic_read(&cifsInfo->inUse) == 0) {
370 inode->i_uid = cifs_sb->mnt_uid;
371 inode->i_gid = cifs_sb->mnt_gid;
372 /* set so we do not keep refreshing these fields with
373 bad data after user has changed them in memory */
374 atomic_set(&cifsInfo->inUse,1);
375 }
376
377 if (S_ISREG(inode->i_mode)) {
378 cFYI(1, (" File inode "));
379 inode->i_op = &cifs_file_inode_ops;
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700380 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 inode->i_fop = &cifs_file_direct_ops;
382 else
383 inode->i_fop = &cifs_file_ops;
Steve Frenchc46fa8a2005-08-18 20:49:57 -0700384 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
385 inode->i_fop->lock = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 inode->i_data.a_ops = &cifs_addr_ops;
387 } else if (S_ISDIR(inode->i_mode)) {
388 cFYI(1, (" Directory inode "));
389 inode->i_op = &cifs_dir_inode_ops;
390 inode->i_fop = &cifs_dir_ops;
391 } else if (S_ISLNK(inode->i_mode)) {
392 cFYI(1, (" Symbolic Link inode "));
393 inode->i_op = &cifs_symlink_inode_ops;
394 } else {
395 init_special_inode(inode, inode->i_mode,
396 inode->i_rdev);
397 }
398 }
399 kfree(buf);
400 return rc;
401}
402
403/* gets root inode */
404void cifs_read_inode(struct inode *inode)
405{
406 int xid;
407 struct cifs_sb_info *cifs_sb;
408
409 cifs_sb = CIFS_SB(inode->i_sb);
410 xid = GetXid();
411 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
412 cifs_get_inode_info_unix(&inode, "", inode->i_sb,xid);
413 else
414 cifs_get_inode_info(&inode, "", NULL, inode->i_sb,xid);
415 /* can not call macro FreeXid here since in a void func */
416 _FreeXid(xid);
417}
418
419int cifs_unlink(struct inode *inode, struct dentry *direntry)
420{
421 int rc = 0;
422 int xid;
423 struct cifs_sb_info *cifs_sb;
424 struct cifsTconInfo *pTcon;
425 char *full_path = NULL;
426 struct cifsInodeInfo *cifsInode;
427 FILE_BASIC_INFO *pinfo_buf;
428
429 cFYI(1, (" cifs_unlink, inode = 0x%p with ", inode));
430
431 xid = GetXid();
432
433 cifs_sb = CIFS_SB(inode->i_sb);
434 pTcon = cifs_sb->tcon;
435
436 /* Unlink can be called from rename so we can not grab the sem here
437 since we deadlock otherwise */
438/* down(&direntry->d_sb->s_vfs_rename_sem);*/
Steve French7f573562005-08-30 11:32:14 -0700439 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/* up(&direntry->d_sb->s_vfs_rename_sem);*/
441 if (full_path == NULL) {
442 FreeXid(xid);
443 return -ENOMEM;
444 }
Steve French737b7582005-04-28 22:41:06 -0700445 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
446 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (!rc) {
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700449 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500450 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 } else if (rc == -ENOENT) {
452 d_drop(direntry);
453 } else if (rc == -ETXTBSY) {
454 int oplock = FALSE;
455 __u16 netfid;
456
457 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
458 CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
Steve French737b7582005-04-28 22:41:06 -0700459 &netfid, &oplock, NULL, cifs_sb->local_nls,
460 cifs_sb->mnt_cifs_flags &
461 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (rc==0) {
463 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
Steve French737b7582005-04-28 22:41:06 -0700464 cifs_sb->local_nls,
465 cifs_sb->mnt_cifs_flags &
466 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700468 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500469 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 } else if (rc == -EACCES) {
472 /* try only if r/o attribute set in local lookup data? */
473 pinfo_buf = kmalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
474 if (pinfo_buf) {
475 memset(pinfo_buf, 0, sizeof(FILE_BASIC_INFO));
476 /* ATTRS set to normal clears r/o bit */
477 pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
478 if (!(pTcon->ses->flags & CIFS_SES_NT4))
479 rc = CIFSSMBSetTimes(xid, pTcon, full_path,
480 pinfo_buf,
Steve French737b7582005-04-28 22:41:06 -0700481 cifs_sb->local_nls,
482 cifs_sb->mnt_cifs_flags &
483 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 else
485 rc = -EOPNOTSUPP;
486
487 if (rc == -EOPNOTSUPP) {
488 int oplock = FALSE;
489 __u16 netfid;
490 /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
491 full_path,
492 (__u16)ATTR_NORMAL,
493 cifs_sb->local_nls);
494 For some strange reason it seems that NT4 eats the
495 old setattr call without actually setting the
496 attributes so on to the third attempted workaround
497 */
498
499 /* BB could scan to see if we already have it open
500 and pass in pid of opener to function */
501 rc = CIFSSMBOpen(xid, pTcon, full_path,
502 FILE_OPEN, SYNCHRONIZE |
503 FILE_WRITE_ATTRIBUTES, 0,
504 &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700505 cifs_sb->local_nls,
506 cifs_sb->mnt_cifs_flags &
507 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (rc==0) {
509 rc = CIFSSMBSetFileTimes(xid, pTcon,
510 pinfo_buf,
511 netfid);
512 CIFSSMBClose(xid, pTcon, netfid);
513 }
514 }
515 kfree(pinfo_buf);
516 }
517 if (rc==0) {
Steve French737b7582005-04-28 22:41:06 -0700518 rc = CIFSSMBDelFile(xid, pTcon, full_path,
519 cifs_sb->local_nls,
520 cifs_sb->mnt_cifs_flags &
521 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!rc) {
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700523 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500524 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 } else if (rc == -ETXTBSY) {
526 int oplock = FALSE;
527 __u16 netfid;
528
529 rc = CIFSSMBOpen(xid, pTcon, full_path,
530 FILE_OPEN, DELETE,
531 CREATE_NOT_DIR |
532 CREATE_DELETE_ON_CLOSE,
533 &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700534 cifs_sb->local_nls,
535 cifs_sb->mnt_cifs_flags &
536 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (rc==0) {
538 CIFSSMBRenameOpenFile(xid, pTcon,
539 netfid, NULL,
Steve French737b7582005-04-28 22:41:06 -0700540 cifs_sb->local_nls,
541 cifs_sb->mnt_cifs_flags &
542 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700544 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500545 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
547 /* BB if rc = -ETXTBUSY goto the rename logic BB */
548 }
549 }
550 }
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700551 if (direntry->d_inode) {
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500552 cifsInode = CIFS_I(direntry->d_inode);
553 cifsInode->time = 0; /* will force revalidate to get info
554 when needed */
555 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
556 }
557 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 cifsInode = CIFS_I(inode);
559 cifsInode->time = 0; /* force revalidate of dir as well */
560
561 kfree(full_path);
562 FreeXid(xid);
563 return rc;
564}
565
566int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
567{
568 int rc = 0;
569 int xid;
570 struct cifs_sb_info *cifs_sb;
571 struct cifsTconInfo *pTcon;
572 char *full_path = NULL;
573 struct inode *newinode = NULL;
574
575 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p ", mode, inode));
576
577 xid = GetXid();
578
579 cifs_sb = CIFS_SB(inode->i_sb);
580 pTcon = cifs_sb->tcon;
581
582 down(&inode->i_sb->s_vfs_rename_sem);
Steve French7f573562005-08-30 11:32:14 -0700583 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 up(&inode->i_sb->s_vfs_rename_sem);
585 if (full_path == NULL) {
586 FreeXid(xid);
587 return -ENOMEM;
588 }
589 /* BB add setting the equivalent of mode via CreateX w/ACLs */
Steve French737b7582005-04-28 22:41:06 -0700590 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
591 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (rc) {
593 cFYI(1, ("cifs_mkdir returned 0x%x ", rc));
594 d_drop(direntry);
595 } else {
596 inode->i_nlink++;
597 if (pTcon->ses->capabilities & CAP_UNIX)
598 rc = cifs_get_inode_info_unix(&newinode, full_path,
599 inode->i_sb,xid);
600 else
601 rc = cifs_get_inode_info(&newinode, full_path, NULL,
602 inode->i_sb,xid);
603
Steve Frenchb92327f2005-08-22 20:09:43 -0700604 if (pTcon->nocase)
605 direntry->d_op = &cifs_ci_dentry_ops;
606 else
607 direntry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 d_instantiate(direntry, newinode);
609 if (direntry->d_inode)
610 direntry->d_inode->i_nlink = 2;
611 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700612 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
614 mode,
615 (__u64)current->euid,
616 (__u64)current->egid,
617 0 /* dev_t */,
Steve French737b7582005-04-28 22:41:06 -0700618 cifs_sb->local_nls,
619 cifs_sb->mnt_cifs_flags &
620 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 } else {
622 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
623 mode, (__u64)-1,
624 (__u64)-1, 0 /* dev_t */,
Steve French737b7582005-04-28 22:41:06 -0700625 cifs_sb->local_nls,
626 cifs_sb->mnt_cifs_flags &
627 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 else {
630 /* BB to be implemented via Windows secrty descriptors
631 eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
632 -1, -1, local_nls); */
633 }
634 }
635 kfree(full_path);
636 FreeXid(xid);
637 return rc;
638}
639
640int cifs_rmdir(struct inode *inode, struct dentry *direntry)
641{
642 int rc = 0;
643 int xid;
644 struct cifs_sb_info *cifs_sb;
645 struct cifsTconInfo *pTcon;
646 char *full_path = NULL;
647 struct cifsInodeInfo *cifsInode;
648
649 cFYI(1, (" cifs_rmdir, inode = 0x%p with ", inode));
650
651 xid = GetXid();
652
653 cifs_sb = CIFS_SB(inode->i_sb);
654 pTcon = cifs_sb->tcon;
655
656 down(&inode->i_sb->s_vfs_rename_sem);
Steve French7f573562005-08-30 11:32:14 -0700657 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 up(&inode->i_sb->s_vfs_rename_sem);
659 if (full_path == NULL) {
660 FreeXid(xid);
661 return -ENOMEM;
662 }
663
Steve French737b7582005-04-28 22:41:06 -0700664 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
665 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (!rc) {
668 inode->i_nlink--;
669 i_size_write(direntry->d_inode,0);
670 direntry->d_inode->i_nlink = 0;
671 }
672
673 cifsInode = CIFS_I(direntry->d_inode);
674 cifsInode->time = 0; /* force revalidate to go get info when
675 needed */
676 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
677 current_fs_time(inode->i_sb);
678
679 kfree(full_path);
680 FreeXid(xid);
681 return rc;
682}
683
684int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
685 struct inode *target_inode, struct dentry *target_direntry)
686{
687 char *fromName;
688 char *toName;
689 struct cifs_sb_info *cifs_sb_source;
690 struct cifs_sb_info *cifs_sb_target;
691 struct cifsTconInfo *pTcon;
692 int xid;
693 int rc = 0;
694
695 xid = GetXid();
696
697 cifs_sb_target = CIFS_SB(target_inode->i_sb);
698 cifs_sb_source = CIFS_SB(source_inode->i_sb);
699 pTcon = cifs_sb_source->tcon;
700
701 if (pTcon != cifs_sb_target->tcon) {
702 FreeXid(xid);
703 return -EXDEV; /* BB actually could be allowed if same server,
704 but different share.
705 Might eventually add support for this */
706 }
707
708 /* we already have the rename sem so we do not need to grab it again
709 here to protect the path integrity */
Steve French7f573562005-08-30 11:32:14 -0700710 fromName = build_path_from_dentry(source_direntry);
711 toName = build_path_from_dentry(target_direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if ((fromName == NULL) || (toName == NULL)) {
713 rc = -ENOMEM;
714 goto cifs_rename_exit;
715 }
716
717 rc = CIFSSMBRename(xid, pTcon, fromName, toName,
Steve French737b7582005-04-28 22:41:06 -0700718 cifs_sb_source->local_nls,
719 cifs_sb_source->mnt_cifs_flags &
720 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (rc == -EEXIST) {
722 /* check if they are the same file because rename of hardlinked
723 files is a noop */
724 FILE_UNIX_BASIC_INFO *info_buf_source;
725 FILE_UNIX_BASIC_INFO *info_buf_target;
726
727 info_buf_source =
728 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
729 if (info_buf_source != NULL) {
730 info_buf_target = info_buf_source + 1;
731 rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
Steve French737b7582005-04-28 22:41:06 -0700732 info_buf_source, cifs_sb_source->local_nls,
733 cifs_sb_source->mnt_cifs_flags &
734 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (rc == 0) {
736 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
737 info_buf_target,
Steve French737b7582005-04-28 22:41:06 -0700738 cifs_sb_target->local_nls,
739 /* remap based on source sb */
740 cifs_sb_source->mnt_cifs_flags &
741 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743 if ((rc == 0) &&
744 (info_buf_source->UniqueId ==
745 info_buf_target->UniqueId)) {
746 /* do not rename since the files are hardlinked which
747 is a noop */
748 } else {
749 /* we either can not tell the files are hardlinked
750 (as with Windows servers) or files are not
751 hardlinked so delete the target manually before
752 renaming to follow POSIX rather than Windows
753 semantics */
754 cifs_unlink(target_inode, target_direntry);
755 rc = CIFSSMBRename(xid, pTcon, fromName,
756 toName,
Steve French737b7582005-04-28 22:41:06 -0700757 cifs_sb_source->local_nls,
758 cifs_sb_source->mnt_cifs_flags
759 & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761 kfree(info_buf_source);
762 } /* if we can not get memory just leave rc as EEXIST */
763 }
764
765 if (rc) {
766 cFYI(1, ("rename rc %d", rc));
767 }
768
769 if ((rc == -EIO) || (rc == -EEXIST)) {
770 int oplock = FALSE;
771 __u16 netfid;
772
773 /* BB FIXME Is Generic Read correct for rename? */
774 /* if renaming directory - we should not say CREATE_NOT_DIR,
775 need to test renaming open directory, also GENERIC_READ
776 might not right be right access to request */
777 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
778 CREATE_NOT_DIR, &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700779 cifs_sb_source->local_nls,
780 cifs_sb_source->mnt_cifs_flags &
781 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (rc==0) {
783 CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
Steve French737b7582005-04-28 22:41:06 -0700784 cifs_sb_source->local_nls,
785 cifs_sb_source->mnt_cifs_flags &
786 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 CIFSSMBClose(xid, pTcon, netfid);
788 }
789 }
790
791cifs_rename_exit:
792 kfree(fromName);
793 kfree(toName);
794 FreeXid(xid);
795 return rc;
796}
797
798int cifs_revalidate(struct dentry *direntry)
799{
800 int xid;
801 int rc = 0;
802 char *full_path;
803 struct cifs_sb_info *cifs_sb;
804 struct cifsInodeInfo *cifsInode;
805 loff_t local_size;
806 struct timespec local_mtime;
807 int invalidate_inode = FALSE;
808
809 if (direntry->d_inode == NULL)
810 return -ENOENT;
811
812 cifsInode = CIFS_I(direntry->d_inode);
813
814 if (cifsInode == NULL)
815 return -ENOENT;
816
817 /* no sense revalidating inode info on file that no one can write */
818 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
819 return rc;
820
821 xid = GetXid();
822
823 cifs_sb = CIFS_SB(direntry->d_sb);
824
825 /* can not safely grab the rename sem here if rename calls revalidate
826 since that would deadlock */
Steve French7f573562005-08-30 11:32:14 -0700827 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (full_path == NULL) {
829 FreeXid(xid);
830 return -ENOMEM;
831 }
832 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
833 "jiffies %ld", full_path, direntry->d_inode,
834 direntry->d_inode->i_count.counter, direntry,
835 direntry->d_time, jiffies));
836
837 if (cifsInode->time == 0) {
838 /* was set to zero previously to force revalidate */
839 } else if (time_before(jiffies, cifsInode->time + HZ) &&
840 lookupCacheEnabled) {
841 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
842 (direntry->d_inode->i_nlink == 1)) {
843 kfree(full_path);
844 FreeXid(xid);
845 return rc;
846 } else {
847 cFYI(1, ("Have to revalidate file due to hardlinks"));
848 }
849 }
850
851 /* save mtime and size */
852 local_mtime = direntry->d_inode->i_mtime;
853 local_size = direntry->d_inode->i_size;
854
855 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
856 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
857 direntry->d_sb,xid);
858 if (rc) {
859 cFYI(1, ("error on getting revalidate info %d", rc));
860/* if (rc != -ENOENT)
861 rc = 0; */ /* BB should we cache info on
862 certain errors? */
863 }
864 } else {
865 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
866 direntry->d_sb,xid);
867 if (rc) {
868 cFYI(1, ("error on getting revalidate info %d", rc));
869/* if (rc != -ENOENT)
870 rc = 0; */ /* BB should we cache info on
871 certain errors? */
872 }
873 }
874 /* should we remap certain errors, access denied?, to zero */
875
876 /* if not oplocked, we invalidate inode pages if mtime or file size
877 had changed on server */
878
879 if (timespec_equal(&local_mtime,&direntry->d_inode->i_mtime) &&
880 (local_size == direntry->d_inode->i_size)) {
881 cFYI(1, ("cifs_revalidate - inode unchanged"));
882 } else {
883 /* file may have changed on server */
884 if (cifsInode->clientCanCacheRead) {
885 /* no need to invalidate inode pages since we were the
886 only ones who could have modified the file and the
887 server copy is staler than ours */
888 } else {
889 invalidate_inode = TRUE;
890 }
891 }
892
893 /* can not grab this sem since kernel filesys locking documentation
894 indicates i_sem may be taken by the kernel on lookup and rename
895 which could deadlock if we grab the i_sem here as well */
896/* down(&direntry->d_inode->i_sem);*/
897 /* need to write out dirty pages here */
898 if (direntry->d_inode->i_mapping) {
899 /* do we need to lock inode until after invalidate completes
900 below? */
901 filemap_fdatawrite(direntry->d_inode->i_mapping);
902 }
903 if (invalidate_inode) {
904 if (direntry->d_inode->i_mapping)
905 filemap_fdatawait(direntry->d_inode->i_mapping);
906 /* may eventually have to do this for open files too */
907 if (list_empty(&(cifsInode->openFileList))) {
908 /* Has changed on server - flush read ahead pages */
909 cFYI(1, ("Invalidating read ahead data on "
910 "closed file"));
911 invalidate_remote_inode(direntry->d_inode);
912 }
913 }
914/* up(&direntry->d_inode->i_sem); */
915
916 kfree(full_path);
917 FreeXid(xid);
918 return rc;
919}
920
921int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
922 struct kstat *stat)
923{
924 int err = cifs_revalidate(dentry);
925 if (!err)
926 generic_fillattr(dentry->d_inode, stat);
927 return err;
928}
929
930static int cifs_truncate_page(struct address_space *mapping, loff_t from)
931{
932 pgoff_t index = from >> PAGE_CACHE_SHIFT;
933 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
934 struct page *page;
935 char *kaddr;
936 int rc = 0;
937
938 page = grab_cache_page(mapping, index);
939 if (!page)
940 return -ENOMEM;
941
942 kaddr = kmap_atomic(page, KM_USER0);
943 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
944 flush_dcache_page(page);
945 kunmap_atomic(kaddr, KM_USER0);
946 unlock_page(page);
947 page_cache_release(page);
948 return rc;
949}
950
951int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
952{
953 int xid;
954 struct cifs_sb_info *cifs_sb;
955 struct cifsTconInfo *pTcon;
956 char *full_path = NULL;
957 int rc = -EACCES;
958 int found = FALSE;
959 struct cifsFileInfo *open_file = NULL;
960 FILE_BASIC_INFO time_buf;
961 int set_time = FALSE;
962 __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
963 __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
964 __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
965 struct cifsInodeInfo *cifsInode;
966 struct list_head *tmp;
967
968 xid = GetXid();
969
970 cFYI(1, (" In cifs_setattr, name = %s attrs->iavalid 0x%x ",
971 direntry->d_name.name, attrs->ia_valid));
972 cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
973 pTcon = cifs_sb->tcon;
974
975 down(&direntry->d_sb->s_vfs_rename_sem);
Steve French7f573562005-08-30 11:32:14 -0700976 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 up(&direntry->d_sb->s_vfs_rename_sem);
978 if (full_path == NULL) {
979 FreeXid(xid);
980 return -ENOMEM;
981 }
982 cifsInode = CIFS_I(direntry->d_inode);
983
984 /* BB check if we need to refresh inode from server now ? BB */
985
986 /* need to flush data before changing file size on server */
987 filemap_fdatawrite(direntry->d_inode->i_mapping);
988 filemap_fdatawait(direntry->d_inode->i_mapping);
989
990 if (attrs->ia_valid & ATTR_SIZE) {
991 read_lock(&GlobalSMBSeslock);
992 /* To avoid spurious oplock breaks from server, in the case of
993 inodes that we already have open, avoid doing path based
994 setting of file size if we can do it by handle.
995 This keeps our caching token (oplock) and avoids timeouts
996 when the local oplock break takes longer to flush
997 writebehind data than the SMB timeout for the SetPathInfo
998 request would allow */
999 list_for_each(tmp, &cifsInode->openFileList) {
1000 open_file = list_entry(tmp, struct cifsFileInfo,
1001 flist);
1002 /* We check if file is open for writing first */
1003 if ((open_file->pfile) &&
1004 ((open_file->pfile->f_flags & O_RDWR) ||
1005 (open_file->pfile->f_flags & O_WRONLY))) {
1006 if (open_file->invalidHandle == FALSE) {
1007 /* we found a valid, writeable network
1008 file handle to use to try to set the
1009 file size */
1010 __u16 nfid = open_file->netfid;
1011 __u32 npid = open_file->pid;
1012 read_unlock(&GlobalSMBSeslock);
1013 found = TRUE;
1014 rc = CIFSSMBSetFileSize(xid, pTcon,
1015 attrs->ia_size, nfid, npid,
1016 FALSE);
1017 cFYI(1, ("SetFileSize by handle "
1018 "(setattrs) rc = %d", rc));
1019 /* Do not need reopen and retry on
1020 EAGAIN since we will retry by
1021 pathname below */
1022
1023 /* now that we found one valid file
1024 handle no sense continuing to loop
1025 trying others, so break here */
1026 break;
1027 }
1028 }
1029 }
1030 if (found == FALSE)
1031 read_unlock(&GlobalSMBSeslock);
1032
1033 if (rc != 0) {
1034 /* Set file size by pathname rather than by handle
1035 either because no valid, writeable file handle for
1036 it was found or because there was an error setting
1037 it by handle */
1038 rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1039 attrs->ia_size, FALSE,
Steve French737b7582005-04-28 22:41:06 -07001040 cifs_sb->local_nls,
1041 cifs_sb->mnt_cifs_flags &
1042 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 cFYI(1, (" SetEOF by path (setattrs) rc = %d", rc));
1044 }
1045
1046 /* Server is ok setting allocation size implicitly - no need
1047 to call:
1048 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
1049 cifs_sb->local_nls);
1050 */
1051
1052 if (rc == 0) {
1053 rc = vmtruncate(direntry->d_inode, attrs->ia_size);
1054 cifs_truncate_page(direntry->d_inode->i_mapping,
1055 direntry->d_inode->i_size);
1056 }
1057 }
1058 if (attrs->ia_valid & ATTR_UID) {
1059 cFYI(1, (" CIFS - UID changed to %d", attrs->ia_uid));
1060 uid = attrs->ia_uid;
1061 /* entry->uid = cpu_to_le16(attr->ia_uid); */
1062 }
1063 if (attrs->ia_valid & ATTR_GID) {
1064 cFYI(1, (" CIFS - GID changed to %d", attrs->ia_gid));
1065 gid = attrs->ia_gid;
1066 /* entry->gid = cpu_to_le16(attr->ia_gid); */
1067 }
1068
1069 time_buf.Attributes = 0;
1070 if (attrs->ia_valid & ATTR_MODE) {
1071 cFYI(1, (" CIFS - Mode changed to 0x%x", attrs->ia_mode));
1072 mode = attrs->ia_mode;
1073 /* entry->mode = cpu_to_le16(attr->ia_mode); */
1074 }
1075
1076 if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX)
1077 && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
1078 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
Steve French737b7582005-04-28 22:41:06 -07001079 0 /* dev_t */, cifs_sb->local_nls,
1080 cifs_sb->mnt_cifs_flags &
1081 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 else if (attrs->ia_valid & ATTR_MODE) {
1083 if ((mode & S_IWUGO) == 0) /* not writeable */ {
1084 if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0)
1085 time_buf.Attributes =
1086 cpu_to_le32(cifsInode->cifsAttrs |
1087 ATTR_READONLY);
1088 } else if ((mode & S_IWUGO) == S_IWUGO) {
1089 if (cifsInode->cifsAttrs & ATTR_READONLY)
1090 time_buf.Attributes =
1091 cpu_to_le32(cifsInode->cifsAttrs &
1092 (~ATTR_READONLY));
1093 }
1094 /* BB to be implemented -
1095 via Windows security descriptors or streams */
1096 /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid,
1097 cifs_sb->local_nls); */
1098 }
1099
1100 if (attrs->ia_valid & ATTR_ATIME) {
1101 set_time = TRUE;
1102 time_buf.LastAccessTime =
1103 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1104 } else
1105 time_buf.LastAccessTime = 0;
1106
1107 if (attrs->ia_valid & ATTR_MTIME) {
1108 set_time = TRUE;
1109 time_buf.LastWriteTime =
1110 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1111 } else
1112 time_buf.LastWriteTime = 0;
1113
1114 if (attrs->ia_valid & ATTR_CTIME) {
1115 set_time = TRUE;
1116 cFYI(1, (" CIFS - CTIME changed ")); /* BB probably no need */
1117 time_buf.ChangeTime =
1118 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1119 } else
1120 time_buf.ChangeTime = 0;
1121
1122 if (set_time || time_buf.Attributes) {
1123 /* BB what if setting one attribute fails (such as size) but
1124 time setting works? */
1125 time_buf.CreationTime = 0; /* do not change */
1126 /* In the future we should experiment - try setting timestamps
1127 via Handle (SetFileInfo) instead of by path */
1128 if (!(pTcon->ses->flags & CIFS_SES_NT4))
1129 rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
Steve French737b7582005-04-28 22:41:06 -07001130 cifs_sb->local_nls,
1131 cifs_sb->mnt_cifs_flags &
1132 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 else
1134 rc = -EOPNOTSUPP;
1135
1136 if (rc == -EOPNOTSUPP) {
1137 int oplock = FALSE;
1138 __u16 netfid;
1139
1140 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
1141 "times not supported by this server"));
1142 /* BB we could scan to see if we already have it open
1143 and pass in pid of opener to function */
1144 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1145 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1146 CREATE_NOT_DIR, &netfid, &oplock,
Steve French737b7582005-04-28 22:41:06 -07001147 NULL, cifs_sb->local_nls,
1148 cifs_sb->mnt_cifs_flags &
1149 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (rc==0) {
1151 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1152 netfid);
1153 CIFSSMBClose(xid, pTcon, netfid);
1154 } else {
1155 /* BB For even older servers we could convert time_buf
1156 into old DOS style which uses two second
1157 granularity */
1158
1159 /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
1160 &time_buf, cifs_sb->local_nls); */
1161 }
1162 }
1163 }
1164
1165 /* do not need local check to inode_check_ok since the server does
1166 that */
1167 if (!rc)
1168 rc = inode_setattr(direntry->d_inode, attrs);
1169 kfree(full_path);
1170 FreeXid(xid);
1171 return rc;
1172}
1173
1174void cifs_delete_inode(struct inode *inode)
1175{
1176 cFYI(1, ("In cifs_delete_inode, inode = 0x%p ", inode));
1177 /* may have to add back in if and when safe distributed caching of
1178 directories added e.g. via FindNotify */
1179}