4 * Copyright (C) International Business Machines Corp., 2002,2005
5 * Author(s): Steve French (sfrench@us.ibm.com)
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.
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.
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
22 #include <linux/buffer_head.h>
23 #include <linux/stat.h>
24 #include <linux/pagemap.h>
25 #include <asm/div64.h>
29 #include "cifsproto.h"
30 #include "cifs_debug.h"
31 #include "cifs_fs_sb.h"
33 int cifs_get_inode_info_unix(struct inode **pinode,
34 const unsigned char *search_path, struct super_block *sb, int xid)
37 FILE_UNIX_BASIC_INFO findData;
38 struct cifsTconInfo *pTcon;
40 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
43 pTcon = cifs_sb->tcon;
44 cFYI(1, (" Getting info on %s ", search_path));
45 /* could have done a find first instead but this returns more info */
46 rc = CIFSSMBUnixQPathInfo(xid, pTcon, search_path, &findData,
47 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
48 CIFS_MOUNT_MAP_SPECIAL_CHR);
49 /* dump_mem("\nUnixQPathInfo return data", &findData,
54 kmalloc(strnlen(pTcon->treeName,
56 strnlen(search_path, MAX_PATHCONF) + 1,
58 if (tmp_path == NULL) {
61 /* have to skip first of the double backslash of
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,
68 cifs_sb->mnt_cifs_flags &
69 CIFS_MOUNT_MAP_SPECIAL_CHR);
72 /* BB fix up inode etc. */
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);
83 if (*pinode == NULL) {
84 *pinode = new_inode(sb);
87 /* Is an i_ino of zero legal? */
88 /* Are there sanity checks we can use to ensure that
89 the server is really filling in that field? */
90 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
92 (unsigned long)findData.UniqueId;
93 } /* note ino incremented to unique num in new_inode */
94 insert_inode_hash(*pinode);
98 cifsInfo = CIFS_I(inode);
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);
107 cifs_NTtimeToUnix(le64_to_cpu(findData.LastAccessTime));
109 cifs_NTtimeToUnix(le64_to_cpu
110 (findData.LastModificationTime));
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;
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);
137 if (is_size_safe_to_change(cifsInfo)) {
138 /* can not safely change the file size here if the
139 client is writing to it due to potential races */
141 i_size_write(inode, end_of_file);
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;*/
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 */
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;
157 if (num_of_bytes < end_of_file)
158 cFYI(1, ("allocation size less than end of file "));
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;
165 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
166 inode->i_fop = &cifs_file_direct_ops;
168 inode->i_fop = &cifs_file_ops;
169 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
170 inode->i_fop->lock = NULL;
171 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 */
181 cFYI(1, (" Init special inode "));
182 init_special_inode(inode, inode->i_mode,
189 int cifs_get_inode_info(struct inode **pinode,
190 const unsigned char *search_path, FILE_ALL_INFO *pfindData,
191 struct super_block *sb, int xid)
194 struct cifsTconInfo *pTcon;
196 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
200 pTcon = cifs_sb->tcon;
201 cFYI(1,("Getting info on %s ", search_path));
203 if ((pfindData == NULL) && (*pinode != NULL)) {
204 if (CIFS_I(*pinode)->clientCanCacheRead) {
205 cFYI(1,("No need to revalidate cached inode sizes"));
210 /* if file info not passed in then get it from server */
211 if (pfindData == NULL) {
212 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
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,
218 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
219 CIFS_MOUNT_MAP_SPECIAL_CHR);
220 /* 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);
231 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
233 if (rc == -EREMOTE) {
238 strnlen(search_path, MAX_PATHCONF) + 1,
240 if (tmp_path == NULL) {
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,
250 cifs_sb->mnt_cifs_flags &
251 CIFS_MOUNT_MAP_SPECIAL_CHR);
253 /* BB fix up inode etc. */
259 struct cifsInodeInfo *cifsInfo;
260 __u32 attr = le32_to_cpu(pfindData->Attributes);
263 if (*pinode == NULL) {
264 *pinode = new_inode(sb);
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? */
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
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? */
282 #ifdef CONFIG_CIFS_EXPERIMENTAL
283 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM){
287 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
288 search_path, &inode_num,
290 cifs_sb->mnt_cifs_flags &
291 CIFS_MOUNT_MAP_SPECIAL_CHR);
293 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);
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));
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;*/
313 /* Linux can not store file creation time unfortunately so we ignore it */
315 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
317 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
319 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
320 cFYI(0, (" Attributes came in as 0x%x ", attr));
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;
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
333 inode->i_mode = cifs_sb->mnt_dir_mode;
334 inode->i_mode |= S_IFDIR;
335 } 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) && ) */
346 inode->i_mode |= S_IFREG;
347 /* treat the dos attribute of read-only as read-only
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? */
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));
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;
365 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
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);
377 if (S_ISREG(inode->i_mode)) {
378 cFYI(1, (" File inode "));
379 inode->i_op = &cifs_file_inode_ops;
380 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO)
381 inode->i_fop = &cifs_file_direct_ops;
383 inode->i_fop = &cifs_file_ops;
384 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
385 inode->i_fop->lock = NULL;
386 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;
395 init_special_inode(inode, inode->i_mode,
403 /* gets root inode */
404 void cifs_read_inode(struct inode *inode)
407 struct cifs_sb_info *cifs_sb;
409 cifs_sb = CIFS_SB(inode->i_sb);
411 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
412 cifs_get_inode_info_unix(&inode, "", inode->i_sb,xid);
414 cifs_get_inode_info(&inode, "", NULL, inode->i_sb,xid);
415 /* can not call macro FreeXid here since in a void func */
419 int cifs_unlink(struct inode *inode, struct dentry *direntry)
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;
429 cFYI(1, (" cifs_unlink, inode = 0x%p with ", inode));
433 cifs_sb = CIFS_SB(inode->i_sb);
434 pTcon = cifs_sb->tcon;
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);*/
439 full_path = build_path_from_dentry(direntry, cifs_sb);
440 /* up(&direntry->d_sb->s_vfs_rename_sem);*/
441 if (full_path == NULL) {
445 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
446 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
449 if (direntry->d_inode)
450 direntry->d_inode->i_nlink--;
451 } else if (rc == -ENOENT) {
453 } else if (rc == -ETXTBSY) {
457 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
458 CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
459 &netfid, &oplock, NULL, cifs_sb->local_nls,
460 cifs_sb->mnt_cifs_flags &
461 CIFS_MOUNT_MAP_SPECIAL_CHR);
463 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
465 cifs_sb->mnt_cifs_flags &
466 CIFS_MOUNT_MAP_SPECIAL_CHR);
467 CIFSSMBClose(xid, pTcon, netfid);
468 if (direntry->d_inode)
469 direntry->d_inode->i_nlink--;
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);
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,
482 cifs_sb->mnt_cifs_flags &
483 CIFS_MOUNT_MAP_SPECIAL_CHR);
487 if (rc == -EOPNOTSUPP) {
490 /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
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
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,
506 cifs_sb->mnt_cifs_flags &
507 CIFS_MOUNT_MAP_SPECIAL_CHR);
509 rc = CIFSSMBSetFileTimes(xid, pTcon,
512 CIFSSMBClose(xid, pTcon, netfid);
518 rc = CIFSSMBDelFile(xid, pTcon, full_path,
520 cifs_sb->mnt_cifs_flags &
521 CIFS_MOUNT_MAP_SPECIAL_CHR);
523 if (direntry->d_inode)
524 direntry->d_inode->i_nlink--;
525 } else if (rc == -ETXTBSY) {
529 rc = CIFSSMBOpen(xid, pTcon, full_path,
532 CREATE_DELETE_ON_CLOSE,
533 &netfid, &oplock, NULL,
535 cifs_sb->mnt_cifs_flags &
536 CIFS_MOUNT_MAP_SPECIAL_CHR);
538 CIFSSMBRenameOpenFile(xid, pTcon,
541 cifs_sb->mnt_cifs_flags &
542 CIFS_MOUNT_MAP_SPECIAL_CHR);
543 CIFSSMBClose(xid, pTcon, netfid);
544 if (direntry->d_inode)
545 direntry->d_inode->i_nlink--;
547 /* BB if rc = -ETXTBUSY goto the rename logic BB */
551 if (direntry->d_inode) {
552 cifsInode = CIFS_I(direntry->d_inode);
553 cifsInode->time = 0; /* will force revalidate to get info
555 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
557 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
558 cifsInode = CIFS_I(inode);
559 cifsInode->time = 0; /* force revalidate of dir as well */
566 int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
570 struct cifs_sb_info *cifs_sb;
571 struct cifsTconInfo *pTcon;
572 char *full_path = NULL;
573 struct inode *newinode = NULL;
575 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p ", mode, inode));
579 cifs_sb = CIFS_SB(inode->i_sb);
580 pTcon = cifs_sb->tcon;
582 down(&inode->i_sb->s_vfs_rename_sem);
583 full_path = build_path_from_dentry(direntry, cifs_sb);
584 up(&inode->i_sb->s_vfs_rename_sem);
585 if (full_path == NULL) {
589 /* BB add setting the equivalent of mode via CreateX w/ACLs */
590 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
591 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
593 cFYI(1, ("cifs_mkdir returned 0x%x ", rc));
597 if (pTcon->ses->capabilities & CAP_UNIX)
598 rc = cifs_get_inode_info_unix(&newinode, full_path,
601 rc = cifs_get_inode_info(&newinode, full_path, NULL,
605 direntry->d_op = &cifs_ci_dentry_ops;
607 direntry->d_op = &cifs_dentry_ops;
608 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)
612 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
613 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
615 (__u64)current->euid,
616 (__u64)current->egid,
619 cifs_sb->mnt_cifs_flags &
620 CIFS_MOUNT_MAP_SPECIAL_CHR);
622 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
624 (__u64)-1, 0 /* dev_t */,
626 cifs_sb->mnt_cifs_flags &
627 CIFS_MOUNT_MAP_SPECIAL_CHR);
630 /* BB to be implemented via Windows secrty descriptors
631 eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
632 -1, -1, local_nls); */
640 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
644 struct cifs_sb_info *cifs_sb;
645 struct cifsTconInfo *pTcon;
646 char *full_path = NULL;
647 struct cifsInodeInfo *cifsInode;
649 cFYI(1, (" cifs_rmdir, inode = 0x%p with ", inode));
653 cifs_sb = CIFS_SB(inode->i_sb);
654 pTcon = cifs_sb->tcon;
656 down(&inode->i_sb->s_vfs_rename_sem);
657 full_path = build_path_from_dentry(direntry, cifs_sb);
658 up(&inode->i_sb->s_vfs_rename_sem);
659 if (full_path == NULL) {
664 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
665 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
669 i_size_write(direntry->d_inode,0);
670 direntry->d_inode->i_nlink = 0;
673 cifsInode = CIFS_I(direntry->d_inode);
674 cifsInode->time = 0; /* force revalidate to go get info when
676 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
677 current_fs_time(inode->i_sb);
684 int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
685 struct inode *target_inode, struct dentry *target_direntry)
689 struct cifs_sb_info *cifs_sb_source;
690 struct cifs_sb_info *cifs_sb_target;
691 struct cifsTconInfo *pTcon;
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;
701 if (pTcon != cifs_sb_target->tcon) {
703 return -EXDEV; /* BB actually could be allowed if same server,
705 Might eventually add support for this */
708 /* we already have the rename sem so we do not need to grab it again
709 here to protect the path integrity */
710 fromName = build_path_from_dentry(source_direntry, cifs_sb_source);
711 toName = build_path_from_dentry(target_direntry, cifs_sb_target);
712 if ((fromName == NULL) || (toName == NULL)) {
714 goto cifs_rename_exit;
717 rc = CIFSSMBRename(xid, pTcon, fromName, toName,
718 cifs_sb_source->local_nls,
719 cifs_sb_source->mnt_cifs_flags &
720 CIFS_MOUNT_MAP_SPECIAL_CHR);
722 /* check if they are the same file because rename of hardlinked
724 FILE_UNIX_BASIC_INFO *info_buf_source;
725 FILE_UNIX_BASIC_INFO *info_buf_target;
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,
732 info_buf_source, cifs_sb_source->local_nls,
733 cifs_sb_source->mnt_cifs_flags &
734 CIFS_MOUNT_MAP_SPECIAL_CHR);
736 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
738 cifs_sb_target->local_nls,
739 /* remap based on source sb */
740 cifs_sb_source->mnt_cifs_flags &
741 CIFS_MOUNT_MAP_SPECIAL_CHR);
744 (info_buf_source->UniqueId ==
745 info_buf_target->UniqueId)) {
746 /* do not rename since the files are hardlinked which
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
754 cifs_unlink(target_inode, target_direntry);
755 rc = CIFSSMBRename(xid, pTcon, fromName,
757 cifs_sb_source->local_nls,
758 cifs_sb_source->mnt_cifs_flags
759 & CIFS_MOUNT_MAP_SPECIAL_CHR);
761 kfree(info_buf_source);
762 } /* if we can not get memory just leave rc as EEXIST */
766 cFYI(1, ("rename rc %d", rc));
769 if ((rc == -EIO) || (rc == -EEXIST)) {
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,
779 cifs_sb_source->local_nls,
780 cifs_sb_source->mnt_cifs_flags &
781 CIFS_MOUNT_MAP_SPECIAL_CHR);
783 CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
784 cifs_sb_source->local_nls,
785 cifs_sb_source->mnt_cifs_flags &
786 CIFS_MOUNT_MAP_SPECIAL_CHR);
787 CIFSSMBClose(xid, pTcon, netfid);
798 int cifs_revalidate(struct dentry *direntry)
803 struct cifs_sb_info *cifs_sb;
804 struct cifsInodeInfo *cifsInode;
806 struct timespec local_mtime;
807 int invalidate_inode = FALSE;
809 if (direntry->d_inode == NULL)
812 cifsInode = CIFS_I(direntry->d_inode);
814 if (cifsInode == NULL)
817 /* no sense revalidating inode info on file that no one can write */
818 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
823 cifs_sb = CIFS_SB(direntry->d_sb);
825 /* can not safely grab the rename sem here if rename calls revalidate
826 since that would deadlock */
827 full_path = build_path_from_dentry(direntry, cifs_sb);
828 if (full_path == NULL) {
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));
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)) {
847 cFYI(1, ("Have to revalidate file due to hardlinks"));
851 /* save mtime and size */
852 local_mtime = direntry->d_inode->i_mtime;
853 local_size = direntry->d_inode->i_size;
855 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
856 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
859 cFYI(1, ("error on getting revalidate info %d", rc));
860 /* if (rc != -ENOENT)
861 rc = 0; */ /* BB should we cache info on
865 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
868 cFYI(1, ("error on getting revalidate info %d", rc));
869 /* if (rc != -ENOENT)
870 rc = 0; */ /* BB should we cache info on
874 /* should we remap certain errors, access denied?, to zero */
876 /* if not oplocked, we invalidate inode pages if mtime or file size
877 had changed on server */
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"));
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 */
889 invalidate_inode = TRUE;
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
901 filemap_fdatawrite(direntry->d_inode->i_mapping);
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 "
911 invalidate_remote_inode(direntry->d_inode);
914 /* up(&direntry->d_inode->i_sem); */
921 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
924 int err = cifs_revalidate(dentry);
926 generic_fillattr(dentry->d_inode, stat);
930 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
932 pgoff_t index = from >> PAGE_CACHE_SHIFT;
933 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
938 page = grab_cache_page(mapping, index);
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);
947 page_cache_release(page);
951 int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
954 struct cifs_sb_info *cifs_sb;
955 struct cifsTconInfo *pTcon;
956 char *full_path = NULL;
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;
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;
975 down(&direntry->d_sb->s_vfs_rename_sem);
976 full_path = build_path_from_dentry(direntry, cifs_sb);
977 up(&direntry->d_sb->s_vfs_rename_sem);
978 if (full_path == NULL) {
982 cifsInode = CIFS_I(direntry->d_inode);
984 /* BB check if we need to refresh inode from server now ? BB */
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);
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,
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
1010 __u16 nfid = open_file->netfid;
1011 __u32 npid = open_file->pid;
1012 read_unlock(&GlobalSMBSeslock);
1014 rc = CIFSSMBSetFileSize(xid, pTcon,
1015 attrs->ia_size, nfid, npid,
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
1023 /* now that we found one valid file
1024 handle no sense continuing to loop
1025 trying others, so break here */
1031 read_unlock(&GlobalSMBSeslock);
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
1038 rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1039 attrs->ia_size, FALSE,
1041 cifs_sb->mnt_cifs_flags &
1042 CIFS_MOUNT_MAP_SPECIAL_CHR);
1043 cFYI(1, (" SetEOF by path (setattrs) rc = %d", rc));
1046 /* Server is ok setting allocation size implicitly - no need
1048 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
1049 cifs_sb->local_nls);
1053 rc = vmtruncate(direntry->d_inode, attrs->ia_size);
1054 cifs_truncate_page(direntry->d_inode->i_mapping,
1055 direntry->d_inode->i_size);
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); */
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); */
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); */
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,
1079 0 /* dev_t */, cifs_sb->local_nls,
1080 cifs_sb->mnt_cifs_flags &
1081 CIFS_MOUNT_MAP_SPECIAL_CHR);
1082 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 |
1088 } else if ((mode & S_IWUGO) == S_IWUGO) {
1089 if (cifsInode->cifsAttrs & ATTR_READONLY)
1090 time_buf.Attributes =
1091 cpu_to_le32(cifsInode->cifsAttrs &
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); */
1100 if (attrs->ia_valid & ATTR_ATIME) {
1102 time_buf.LastAccessTime =
1103 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1105 time_buf.LastAccessTime = 0;
1107 if (attrs->ia_valid & ATTR_MTIME) {
1109 time_buf.LastWriteTime =
1110 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1112 time_buf.LastWriteTime = 0;
1114 if (attrs->ia_valid & ATTR_CTIME) {
1116 cFYI(1, (" CIFS - CTIME changed ")); /* BB probably no need */
1117 time_buf.ChangeTime =
1118 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1120 time_buf.ChangeTime = 0;
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,
1131 cifs_sb->mnt_cifs_flags &
1132 CIFS_MOUNT_MAP_SPECIAL_CHR);
1136 if (rc == -EOPNOTSUPP) {
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,
1147 NULL, cifs_sb->local_nls,
1148 cifs_sb->mnt_cifs_flags &
1149 CIFS_MOUNT_MAP_SPECIAL_CHR);
1151 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1153 CIFSSMBClose(xid, pTcon, netfid);
1155 /* BB For even older servers we could convert time_buf
1156 into old DOS style which uses two second
1159 /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
1160 &time_buf, cifs_sb->local_nls); */
1165 /* do not need local check to inode_check_ok since the server does
1168 rc = inode_setattr(direntry->d_inode, attrs);
1174 void cifs_delete_inode(struct inode *inode)
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 */