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 /* since we set the inode type below we need to mask off
115 to avoid strange results if bits set above */
116 inode->i_mode &= ~S_IFMT;
117 if (type == UNIX_FILE) {
118 inode->i_mode |= S_IFREG;
119 } else if (type == UNIX_SYMLINK) {
120 inode->i_mode |= S_IFLNK;
121 } else if (type == UNIX_DIR) {
122 inode->i_mode |= S_IFDIR;
123 } else if (type == UNIX_CHARDEV) {
124 inode->i_mode |= S_IFCHR;
125 inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
126 le64_to_cpu(findData.DevMinor) & MINORMASK);
127 } else if (type == UNIX_BLOCKDEV) {
128 inode->i_mode |= S_IFBLK;
129 inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
130 le64_to_cpu(findData.DevMinor) & MINORMASK);
131 } else if (type == UNIX_FIFO) {
132 inode->i_mode |= S_IFIFO;
133 } else if (type == UNIX_SOCKET) {
134 inode->i_mode |= S_IFSOCK;
136 /* safest to call it a file if we do not know */
137 inode->i_mode |= S_IFREG;
138 cFYI(1,("unknown type %d",type));
140 inode->i_uid = le64_to_cpu(findData.Uid);
141 inode->i_gid = le64_to_cpu(findData.Gid);
142 inode->i_nlink = le64_to_cpu(findData.Nlinks);
144 if (is_size_safe_to_change(cifsInfo)) {
145 /* can not safely change the file size here if the
146 client is writing to it due to potential races */
148 i_size_write(inode, end_of_file);
150 /* blksize needs to be multiple of two. So safer to default to
151 blksize and blkbits set in superblock so 2**blkbits and blksize
152 will match rather than setting to:
153 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
155 /* This seems incredibly stupid but it turns out that i_blocks
156 is not related to (i_size / i_blksize), instead 512 byte size
157 is required for calculating num blocks */
159 /* 512 bytes (2**9) is the fake blocksize that must be used */
160 /* for this calculation */
161 inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
164 if (num_of_bytes < end_of_file)
165 cFYI(1, ("allocation size less than end of file"));
167 ("Size %ld and blocks %ld",
168 (unsigned long) inode->i_size, inode->i_blocks));
169 if (S_ISREG(inode->i_mode)) {
170 cFYI(1, ("File inode"));
171 inode->i_op = &cifs_file_inode_ops;
172 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
173 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
175 &cifs_file_direct_nobrl_ops;
177 inode->i_fop = &cifs_file_direct_ops;
178 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
179 inode->i_fop = &cifs_file_nobrl_ops;
180 else /* not direct, send byte range locks */
181 inode->i_fop = &cifs_file_ops;
183 inode->i_data.a_ops = &cifs_addr_ops;
184 /* check if server can support readpages */
185 if(pTcon->ses->server->maxBuf <
186 4096 + MAX_CIFS_HDR_SIZE)
187 inode->i_data.a_ops->readpages = NULL;
188 } else if (S_ISDIR(inode->i_mode)) {
189 cFYI(1, ("Directory inode"));
190 inode->i_op = &cifs_dir_inode_ops;
191 inode->i_fop = &cifs_dir_ops;
192 } else if (S_ISLNK(inode->i_mode)) {
193 cFYI(1, ("Symbolic Link inode"));
194 inode->i_op = &cifs_symlink_inode_ops;
195 /* tmp_inode->i_fop = */ /* do not need to set to anything */
197 cFYI(1, ("Init special inode"));
198 init_special_inode(inode, inode->i_mode,
205 static int decode_sfu_inode(struct inode * inode, __u64 size,
206 const unsigned char *path,
207 struct cifs_sb_info *cifs_sb, int xid)
212 struct cifsTconInfo *pTcon = cifs_sb->tcon;
214 unsigned int bytes_read;
220 inode->i_mode |= S_IFIFO;
222 } else if (size < 8) {
223 return -EINVAL; /* EOPNOTSUPP? */
226 rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
227 CREATE_NOT_DIR, &netfid, &oplock, NULL,
229 cifs_sb->mnt_cifs_flags &
230 CIFS_MOUNT_MAP_SPECIAL_CHR);
233 rc = CIFSSMBRead(xid, pTcon,
235 8 /* length */, 0 /* offset */,
237 if((rc == 0) && (bytes_read == 8)) {
238 if(memcmp("IntxBLK", pbuf, 8) == 0) {
239 cFYI(1,("Block device"));
240 inode->i_mode |= S_IFBLK;
241 } else if(memcmp("IntxCHR", pbuf, 8) == 0) {
242 cFYI(1,("Char device"));
243 inode->i_mode |= S_IFCHR;
244 } else if(memcmp("IntxLNK", pbuf, 7) == 0) {
246 inode->i_mode |= S_IFLNK;
249 inode->i_mode |= S_IFREG; /* then it is a file */
250 rc = -EOPNOTSUPP; /* or some unknown SFU type */
253 CIFSSMBClose(xid, pTcon, netfid);
256 /* inode->i_rdev = MKDEV(le64_to_cpu(DevMajor),
257 le64_to_cpu(DevMinor) & MINORMASK);*/
258 /* inode->i_mode |= S_IFBLK; */
264 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
266 static int get_sfu_uid_mode(struct inode * inode,
267 const unsigned char *path,
268 struct cifs_sb_info *cifs_sb, int xid)
270 #ifdef CONFIG_CIFS_XATTR
275 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
276 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
277 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
281 mode = le32_to_cpu(*((__le32 *)ea_value));
282 inode->i_mode &= ~SFBITS_MASK;
283 cFYI(1,("special bits 0%o org mode 0%o", mode, inode->i_mode));
284 inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
285 cFYI(1,("special mode bits 0%o", mode));
297 int cifs_get_inode_info(struct inode **pinode,
298 const unsigned char *search_path, FILE_ALL_INFO *pfindData,
299 struct super_block *sb, int xid)
302 struct cifsTconInfo *pTcon;
304 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
308 pTcon = cifs_sb->tcon;
309 cFYI(1,("Getting info on %s", search_path));
311 if ((pfindData == NULL) && (*pinode != NULL)) {
312 if (CIFS_I(*pinode)->clientCanCacheRead) {
313 cFYI(1,("No need to revalidate cached inode sizes"));
318 /* if file info not passed in then get it from server */
319 if (pfindData == NULL) {
320 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
323 pfindData = (FILE_ALL_INFO *)buf;
324 /* could do find first instead but this returns more info */
325 rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
326 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
327 CIFS_MOUNT_MAP_SPECIAL_CHR);
328 /* BB optimize code so we do not make the above call
329 when server claims no NT SMB support and the above call
330 failed at least once - set flag in tcon or mount */
331 if((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
332 rc = SMBQueryInformation(xid, pTcon, search_path,
333 pfindData, cifs_sb->local_nls,
334 cifs_sb->mnt_cifs_flags &
335 CIFS_MOUNT_MAP_SPECIAL_CHR);
339 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
341 if (rc == -EREMOTE) {
346 strnlen(search_path, MAX_PATHCONF) + 1,
348 if (tmp_path == NULL) {
353 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
354 strncat(tmp_path, search_path, MAX_PATHCONF);
355 rc = connect_to_dfs_path(xid, pTcon->ses,
356 /* treename + */ tmp_path,
358 cifs_sb->mnt_cifs_flags &
359 CIFS_MOUNT_MAP_SPECIAL_CHR);
361 /* BB fix up inode etc. */
367 struct cifsInodeInfo *cifsInfo;
368 __u32 attr = le32_to_cpu(pfindData->Attributes);
371 if (*pinode == NULL) {
372 *pinode = new_inode(sb);
375 /* Is an i_ino of zero legal? Can we use that to check
376 if the server supports returning inode numbers? Are
377 there other sanity checks we can use to ensure that
378 the server is really filling in that field? */
380 /* We can not use the IndexNumber field by default from
381 Windows or Samba (in ALL_INFO buf) but we can request
382 it explicitly. It may not be unique presumably if
383 the server has multiple devices mounted under one
386 /* There may be higher info levels that work but are
387 there Windows server or network appliances for which
388 IndexNumber field is not guaranteed unique? */
390 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM){
394 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
395 search_path, &inode_num,
397 cifs_sb->mnt_cifs_flags &
398 CIFS_MOUNT_MAP_SPECIAL_CHR);
400 cFYI(1,("GetSrvInodeNum rc %d", rc1));
401 /* BB EOPNOSUPP disable SERVER_INUM? */
402 } else /* do we need cast or hash to ino? */
403 (*pinode)->i_ino = inode_num;
404 } /* else ino incremented to unique num in new_inode*/
405 insert_inode_hash(*pinode);
408 cifsInfo = CIFS_I(inode);
409 cifsInfo->cifsAttrs = attr;
410 cFYI(1, ("Old time %ld ", cifsInfo->time));
411 cifsInfo->time = jiffies;
412 cFYI(1, ("New time %ld ", cifsInfo->time));
414 /* blksize needs to be multiple of two. So safer to default to
415 blksize and blkbits set in superblock so 2**blkbits and blksize
416 will match rather than setting to:
417 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
419 /* Linux can not store file creation time unfortunately so we ignore it */
421 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
423 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
425 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
426 cFYI(0, ("Attributes came in as 0x%x ", attr));
428 /* set default mode. will override for dirs below */
429 if (atomic_read(&cifsInfo->inUse) == 0)
430 /* new inode, can safely set these fields */
431 inode->i_mode = cifs_sb->mnt_file_mode;
432 else /* since we set the inode type below we need to mask off
433 to avoid strange results if type changes and both get orred in */
434 inode->i_mode &= ~S_IFMT;
435 /* if (attr & ATTR_REPARSE) */
436 /* We no longer handle these as symlinks because we could not
437 follow them due to the absolute path with drive letter */
438 if (attr & ATTR_DIRECTORY) {
439 /* override default perms since we do not do byte range locking
441 inode->i_mode = cifs_sb->mnt_dir_mode;
442 inode->i_mode |= S_IFDIR;
443 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
444 (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
445 /* No need to le64 convert size of zero */
446 (pfindData->EndOfFile == 0)) {
447 inode->i_mode = cifs_sb->mnt_file_mode;
448 inode->i_mode |= S_IFIFO;
449 /* BB Finish for SFU style symlinks and devices */
450 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
451 (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
452 if (decode_sfu_inode(inode,
453 le64_to_cpu(pfindData->EndOfFile),
456 cFYI(1,("Unrecognized sfu inode type"));
458 cFYI(1,("sfu mode 0%o",inode->i_mode));
460 inode->i_mode |= S_IFREG;
461 /* treat the dos attribute of read-only as read-only
463 if (cifsInfo->cifsAttrs & ATTR_READONLY)
464 inode->i_mode &= ~(S_IWUGO);
465 /* BB add code here -
466 validate if device or weird share or device type? */
468 if (is_size_safe_to_change(cifsInfo)) {
469 /* can not safely change the file size here if the
470 client is writing to it due to potential races */
471 i_size_write(inode,le64_to_cpu(pfindData->EndOfFile));
473 /* 512 bytes (2**9) is the fake blocksize that must be
474 used for this calculation */
475 inode->i_blocks = (512 - 1 + le64_to_cpu(
476 pfindData->AllocationSize)) >> 9;
479 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
481 /* BB fill in uid and gid here? with help from winbind?
482 or retrieve from NTFS stream extended attribute */
483 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
484 /* fill in uid, gid, mode from server ACL */
485 get_sfu_uid_mode(inode, search_path, cifs_sb, xid);
486 } else if (atomic_read(&cifsInfo->inUse) == 0) {
487 inode->i_uid = cifs_sb->mnt_uid;
488 inode->i_gid = cifs_sb->mnt_gid;
489 /* set so we do not keep refreshing these fields with
490 bad data after user has changed them in memory */
491 atomic_set(&cifsInfo->inUse,1);
494 if (S_ISREG(inode->i_mode)) {
495 cFYI(1, ("File inode"));
496 inode->i_op = &cifs_file_inode_ops;
497 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
498 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
500 &cifs_file_direct_nobrl_ops;
502 inode->i_fop = &cifs_file_direct_ops;
503 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
504 inode->i_fop = &cifs_file_nobrl_ops;
505 else /* not direct, send byte range locks */
506 inode->i_fop = &cifs_file_ops;
508 inode->i_data.a_ops = &cifs_addr_ops;
509 if(pTcon->ses->server->maxBuf <
510 4096 + MAX_CIFS_HDR_SIZE)
511 inode->i_data.a_ops->readpages = NULL;
512 } else if (S_ISDIR(inode->i_mode)) {
513 cFYI(1, ("Directory inode"));
514 inode->i_op = &cifs_dir_inode_ops;
515 inode->i_fop = &cifs_dir_ops;
516 } else if (S_ISLNK(inode->i_mode)) {
517 cFYI(1, ("Symbolic Link inode"));
518 inode->i_op = &cifs_symlink_inode_ops;
520 init_special_inode(inode, inode->i_mode,
528 /* gets root inode */
529 void cifs_read_inode(struct inode *inode)
532 struct cifs_sb_info *cifs_sb;
534 cifs_sb = CIFS_SB(inode->i_sb);
536 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
537 cifs_get_inode_info_unix(&inode, "", inode->i_sb,xid);
539 cifs_get_inode_info(&inode, "", NULL, inode->i_sb,xid);
540 /* can not call macro FreeXid here since in a void func */
544 int cifs_unlink(struct inode *inode, struct dentry *direntry)
548 struct cifs_sb_info *cifs_sb;
549 struct cifsTconInfo *pTcon;
550 char *full_path = NULL;
551 struct cifsInodeInfo *cifsInode;
552 FILE_BASIC_INFO *pinfo_buf;
554 cFYI(1, ("cifs_unlink, inode = 0x%p with ", inode));
558 cifs_sb = CIFS_SB(inode->i_sb);
559 pTcon = cifs_sb->tcon;
561 /* Unlink can be called from rename so we can not grab the sem here
562 since we deadlock otherwise */
563 /* down(&direntry->d_sb->s_vfs_rename_sem);*/
564 full_path = build_path_from_dentry(direntry);
565 /* up(&direntry->d_sb->s_vfs_rename_sem);*/
566 if (full_path == NULL) {
570 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
571 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
574 if (direntry->d_inode)
575 direntry->d_inode->i_nlink--;
576 } else if (rc == -ENOENT) {
578 } else if (rc == -ETXTBSY) {
582 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
583 CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
584 &netfid, &oplock, NULL, cifs_sb->local_nls,
585 cifs_sb->mnt_cifs_flags &
586 CIFS_MOUNT_MAP_SPECIAL_CHR);
588 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
590 cifs_sb->mnt_cifs_flags &
591 CIFS_MOUNT_MAP_SPECIAL_CHR);
592 CIFSSMBClose(xid, pTcon, netfid);
593 if (direntry->d_inode)
594 direntry->d_inode->i_nlink--;
596 } else if (rc == -EACCES) {
597 /* try only if r/o attribute set in local lookup data? */
598 pinfo_buf = kmalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
600 memset(pinfo_buf, 0, sizeof(FILE_BASIC_INFO));
601 /* ATTRS set to normal clears r/o bit */
602 pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
603 if (!(pTcon->ses->flags & CIFS_SES_NT4))
604 rc = CIFSSMBSetTimes(xid, pTcon, full_path,
607 cifs_sb->mnt_cifs_flags &
608 CIFS_MOUNT_MAP_SPECIAL_CHR);
612 if (rc == -EOPNOTSUPP) {
615 /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
619 For some strange reason it seems that NT4 eats the
620 old setattr call without actually setting the
621 attributes so on to the third attempted workaround
624 /* BB could scan to see if we already have it open
625 and pass in pid of opener to function */
626 rc = CIFSSMBOpen(xid, pTcon, full_path,
627 FILE_OPEN, SYNCHRONIZE |
628 FILE_WRITE_ATTRIBUTES, 0,
629 &netfid, &oplock, NULL,
631 cifs_sb->mnt_cifs_flags &
632 CIFS_MOUNT_MAP_SPECIAL_CHR);
634 rc = CIFSSMBSetFileTimes(xid, pTcon,
637 CIFSSMBClose(xid, pTcon, netfid);
643 rc = CIFSSMBDelFile(xid, pTcon, full_path,
645 cifs_sb->mnt_cifs_flags &
646 CIFS_MOUNT_MAP_SPECIAL_CHR);
648 if (direntry->d_inode)
649 direntry->d_inode->i_nlink--;
650 } else if (rc == -ETXTBSY) {
654 rc = CIFSSMBOpen(xid, pTcon, full_path,
657 CREATE_DELETE_ON_CLOSE,
658 &netfid, &oplock, NULL,
660 cifs_sb->mnt_cifs_flags &
661 CIFS_MOUNT_MAP_SPECIAL_CHR);
663 CIFSSMBRenameOpenFile(xid, pTcon,
666 cifs_sb->mnt_cifs_flags &
667 CIFS_MOUNT_MAP_SPECIAL_CHR);
668 CIFSSMBClose(xid, pTcon, netfid);
669 if (direntry->d_inode)
670 direntry->d_inode->i_nlink--;
672 /* BB if rc = -ETXTBUSY goto the rename logic BB */
676 if (direntry->d_inode) {
677 cifsInode = CIFS_I(direntry->d_inode);
678 cifsInode->time = 0; /* will force revalidate to get info
680 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
682 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
683 cifsInode = CIFS_I(inode);
684 cifsInode->time = 0; /* force revalidate of dir as well */
691 int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
695 struct cifs_sb_info *cifs_sb;
696 struct cifsTconInfo *pTcon;
697 char *full_path = NULL;
698 struct inode *newinode = NULL;
700 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p ", mode, inode));
704 cifs_sb = CIFS_SB(inode->i_sb);
705 pTcon = cifs_sb->tcon;
707 down(&inode->i_sb->s_vfs_rename_sem);
708 full_path = build_path_from_dentry(direntry);
709 up(&inode->i_sb->s_vfs_rename_sem);
710 if (full_path == NULL) {
714 /* BB add setting the equivalent of mode via CreateX w/ACLs */
715 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
716 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
718 cFYI(1, ("cifs_mkdir returned 0x%x ", rc));
722 if (pTcon->ses->capabilities & CAP_UNIX)
723 rc = cifs_get_inode_info_unix(&newinode, full_path,
726 rc = cifs_get_inode_info(&newinode, full_path, NULL,
730 direntry->d_op = &cifs_ci_dentry_ops;
732 direntry->d_op = &cifs_dentry_ops;
733 d_instantiate(direntry, newinode);
734 if (direntry->d_inode)
735 direntry->d_inode->i_nlink = 2;
736 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
737 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
738 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
740 (__u64)current->euid,
741 (__u64)current->egid,
744 cifs_sb->mnt_cifs_flags &
745 CIFS_MOUNT_MAP_SPECIAL_CHR);
747 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
749 (__u64)-1, 0 /* dev_t */,
751 cifs_sb->mnt_cifs_flags &
752 CIFS_MOUNT_MAP_SPECIAL_CHR);
755 /* BB to be implemented via Windows secrty descriptors
756 eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
757 -1, -1, local_nls); */
765 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
769 struct cifs_sb_info *cifs_sb;
770 struct cifsTconInfo *pTcon;
771 char *full_path = NULL;
772 struct cifsInodeInfo *cifsInode;
774 cFYI(1, ("cifs_rmdir, inode = 0x%p with ", inode));
778 cifs_sb = CIFS_SB(inode->i_sb);
779 pTcon = cifs_sb->tcon;
781 down(&inode->i_sb->s_vfs_rename_sem);
782 full_path = build_path_from_dentry(direntry);
783 up(&inode->i_sb->s_vfs_rename_sem);
784 if (full_path == NULL) {
789 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
790 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
794 i_size_write(direntry->d_inode,0);
795 direntry->d_inode->i_nlink = 0;
798 cifsInode = CIFS_I(direntry->d_inode);
799 cifsInode->time = 0; /* force revalidate to go get info when
801 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
802 current_fs_time(inode->i_sb);
809 int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
810 struct inode *target_inode, struct dentry *target_direntry)
814 struct cifs_sb_info *cifs_sb_source;
815 struct cifs_sb_info *cifs_sb_target;
816 struct cifsTconInfo *pTcon;
822 cifs_sb_target = CIFS_SB(target_inode->i_sb);
823 cifs_sb_source = CIFS_SB(source_inode->i_sb);
824 pTcon = cifs_sb_source->tcon;
826 if (pTcon != cifs_sb_target->tcon) {
828 return -EXDEV; /* BB actually could be allowed if same server,
830 Might eventually add support for this */
833 /* we already have the rename sem so we do not need to grab it again
834 here to protect the path integrity */
835 fromName = build_path_from_dentry(source_direntry);
836 toName = build_path_from_dentry(target_direntry);
837 if ((fromName == NULL) || (toName == NULL)) {
839 goto cifs_rename_exit;
842 rc = CIFSSMBRename(xid, pTcon, fromName, toName,
843 cifs_sb_source->local_nls,
844 cifs_sb_source->mnt_cifs_flags &
845 CIFS_MOUNT_MAP_SPECIAL_CHR);
847 /* check if they are the same file because rename of hardlinked
849 FILE_UNIX_BASIC_INFO *info_buf_source;
850 FILE_UNIX_BASIC_INFO *info_buf_target;
853 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
854 if (info_buf_source != NULL) {
855 info_buf_target = info_buf_source + 1;
856 rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
857 info_buf_source, cifs_sb_source->local_nls,
858 cifs_sb_source->mnt_cifs_flags &
859 CIFS_MOUNT_MAP_SPECIAL_CHR);
861 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
863 cifs_sb_target->local_nls,
864 /* remap based on source sb */
865 cifs_sb_source->mnt_cifs_flags &
866 CIFS_MOUNT_MAP_SPECIAL_CHR);
869 (info_buf_source->UniqueId ==
870 info_buf_target->UniqueId)) {
871 /* do not rename since the files are hardlinked which
874 /* we either can not tell the files are hardlinked
875 (as with Windows servers) or files are not
876 hardlinked so delete the target manually before
877 renaming to follow POSIX rather than Windows
879 cifs_unlink(target_inode, target_direntry);
880 rc = CIFSSMBRename(xid, pTcon, fromName,
882 cifs_sb_source->local_nls,
883 cifs_sb_source->mnt_cifs_flags
884 & CIFS_MOUNT_MAP_SPECIAL_CHR);
886 kfree(info_buf_source);
887 } /* if we can not get memory just leave rc as EEXIST */
891 cFYI(1, ("rename rc %d", rc));
894 if ((rc == -EIO) || (rc == -EEXIST)) {
898 /* BB FIXME Is Generic Read correct for rename? */
899 /* if renaming directory - we should not say CREATE_NOT_DIR,
900 need to test renaming open directory, also GENERIC_READ
901 might not right be right access to request */
902 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
903 CREATE_NOT_DIR, &netfid, &oplock, NULL,
904 cifs_sb_source->local_nls,
905 cifs_sb_source->mnt_cifs_flags &
906 CIFS_MOUNT_MAP_SPECIAL_CHR);
908 CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
909 cifs_sb_source->local_nls,
910 cifs_sb_source->mnt_cifs_flags &
911 CIFS_MOUNT_MAP_SPECIAL_CHR);
912 CIFSSMBClose(xid, pTcon, netfid);
923 int cifs_revalidate(struct dentry *direntry)
928 struct cifs_sb_info *cifs_sb;
929 struct cifsInodeInfo *cifsInode;
931 struct timespec local_mtime;
932 int invalidate_inode = FALSE;
934 if (direntry->d_inode == NULL)
937 cifsInode = CIFS_I(direntry->d_inode);
939 if (cifsInode == NULL)
942 /* no sense revalidating inode info on file that no one can write */
943 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
948 cifs_sb = CIFS_SB(direntry->d_sb);
950 /* can not safely grab the rename sem here if rename calls revalidate
951 since that would deadlock */
952 full_path = build_path_from_dentry(direntry);
953 if (full_path == NULL) {
957 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
958 "jiffies %ld", full_path, direntry->d_inode,
959 direntry->d_inode->i_count.counter, direntry,
960 direntry->d_time, jiffies));
962 if (cifsInode->time == 0) {
963 /* was set to zero previously to force revalidate */
964 } else if (time_before(jiffies, cifsInode->time + HZ) &&
965 lookupCacheEnabled) {
966 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
967 (direntry->d_inode->i_nlink == 1)) {
972 cFYI(1, ("Have to revalidate file due to hardlinks"));
976 /* save mtime and size */
977 local_mtime = direntry->d_inode->i_mtime;
978 local_size = direntry->d_inode->i_size;
980 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
981 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
984 cFYI(1, ("error on getting revalidate info %d", rc));
985 /* if (rc != -ENOENT)
986 rc = 0; */ /* BB should we cache info on
990 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
993 cFYI(1, ("error on getting revalidate info %d", rc));
994 /* if (rc != -ENOENT)
995 rc = 0; */ /* BB should we cache info on
999 /* should we remap certain errors, access denied?, to zero */
1001 /* if not oplocked, we invalidate inode pages if mtime or file size
1002 had changed on server */
1004 if (timespec_equal(&local_mtime,&direntry->d_inode->i_mtime) &&
1005 (local_size == direntry->d_inode->i_size)) {
1006 cFYI(1, ("cifs_revalidate - inode unchanged"));
1008 /* file may have changed on server */
1009 if (cifsInode->clientCanCacheRead) {
1010 /* no need to invalidate inode pages since we were the
1011 only ones who could have modified the file and the
1012 server copy is staler than ours */
1014 invalidate_inode = TRUE;
1018 /* can not grab this sem since kernel filesys locking documentation
1019 indicates i_sem may be taken by the kernel on lookup and rename
1020 which could deadlock if we grab the i_sem here as well */
1021 /* down(&direntry->d_inode->i_sem);*/
1022 /* need to write out dirty pages here */
1023 if (direntry->d_inode->i_mapping) {
1024 /* do we need to lock inode until after invalidate completes
1026 filemap_fdatawrite(direntry->d_inode->i_mapping);
1028 if (invalidate_inode) {
1029 if (direntry->d_inode->i_mapping)
1030 filemap_fdatawait(direntry->d_inode->i_mapping);
1031 /* may eventually have to do this for open files too */
1032 if (list_empty(&(cifsInode->openFileList))) {
1033 /* Has changed on server - flush read ahead pages */
1034 cFYI(1, ("Invalidating read ahead data on "
1036 invalidate_remote_inode(direntry->d_inode);
1039 /* up(&direntry->d_inode->i_sem); */
1046 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1049 int err = cifs_revalidate(dentry);
1051 generic_fillattr(dentry->d_inode, stat);
1055 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1057 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1058 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1063 page = grab_cache_page(mapping, index);
1067 kaddr = kmap_atomic(page, KM_USER0);
1068 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1069 flush_dcache_page(page);
1070 kunmap_atomic(kaddr, KM_USER0);
1072 page_cache_release(page);
1076 int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
1079 struct cifs_sb_info *cifs_sb;
1080 struct cifsTconInfo *pTcon;
1081 char *full_path = NULL;
1083 struct cifsFileInfo *open_file = NULL;
1084 FILE_BASIC_INFO time_buf;
1085 int set_time = FALSE;
1086 __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
1087 __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
1088 __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
1089 struct cifsInodeInfo *cifsInode;
1093 cFYI(1, ("In cifs_setattr, name = %s attrs->iavalid 0x%x ",
1094 direntry->d_name.name, attrs->ia_valid));
1095 cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
1096 pTcon = cifs_sb->tcon;
1098 down(&direntry->d_sb->s_vfs_rename_sem);
1099 full_path = build_path_from_dentry(direntry);
1100 up(&direntry->d_sb->s_vfs_rename_sem);
1101 if (full_path == NULL) {
1105 cifsInode = CIFS_I(direntry->d_inode);
1107 /* BB check if we need to refresh inode from server now ? BB */
1109 /* need to flush data before changing file size on server */
1110 filemap_fdatawrite(direntry->d_inode->i_mapping);
1111 filemap_fdatawait(direntry->d_inode->i_mapping);
1113 if (attrs->ia_valid & ATTR_SIZE) {
1114 /* To avoid spurious oplock breaks from server, in the case of
1115 inodes that we already have open, avoid doing path based
1116 setting of file size if we can do it by handle.
1117 This keeps our caching token (oplock) and avoids timeouts
1118 when the local oplock break takes longer to flush
1119 writebehind data than the SMB timeout for the SetPathInfo
1120 request would allow */
1121 open_file = find_writable_file(cifsInode);
1123 __u16 nfid = open_file->netfid;
1124 __u32 npid = open_file->pid;
1125 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
1127 atomic_dec(&open_file->wrtPending);
1128 cFYI(1,("SetFSize for attrs rc = %d", rc));
1131 rc = CIFSSMBWrite(xid, pTcon,
1132 nfid, 0, attrs->ia_size,
1133 &bytes_written, NULL, NULL,
1134 1 /* 45 seconds */);
1135 cFYI(1,("Wrt seteof rc %d", rc));
1139 /* Set file size by pathname rather than by handle
1140 either because no valid, writeable file handle for
1141 it was found or because there was an error setting
1143 rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1144 attrs->ia_size, FALSE,
1146 cifs_sb->mnt_cifs_flags &
1147 CIFS_MOUNT_MAP_SPECIAL_CHR);
1148 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1153 rc = SMBLegacyOpen(xid, pTcon, full_path,
1155 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1156 CREATE_NOT_DIR, &netfid, &oplock,
1157 NULL, cifs_sb->local_nls,
1158 cifs_sb->mnt_cifs_flags &
1159 CIFS_MOUNT_MAP_SPECIAL_CHR);
1162 rc = CIFSSMBWrite(xid, pTcon,
1165 &bytes_written, NULL,
1166 NULL, 1 /* 45 sec */);
1167 cFYI(1,("wrt seteof rc %d",rc));
1168 CIFSSMBClose(xid, pTcon, netfid);
1174 /* Server is ok setting allocation size implicitly - no need
1176 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
1177 cifs_sb->local_nls);
1181 rc = vmtruncate(direntry->d_inode, attrs->ia_size);
1182 cifs_truncate_page(direntry->d_inode->i_mapping,
1183 direntry->d_inode->i_size);
1185 goto cifs_setattr_exit;
1187 if (attrs->ia_valid & ATTR_UID) {
1188 cFYI(1, ("UID changed to %d", attrs->ia_uid));
1189 uid = attrs->ia_uid;
1191 if (attrs->ia_valid & ATTR_GID) {
1192 cFYI(1, ("GID changed to %d", attrs->ia_gid));
1193 gid = attrs->ia_gid;
1196 time_buf.Attributes = 0;
1197 if (attrs->ia_valid & ATTR_MODE) {
1198 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
1199 mode = attrs->ia_mode;
1202 if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX)
1203 && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
1204 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
1205 0 /* dev_t */, cifs_sb->local_nls,
1206 cifs_sb->mnt_cifs_flags &
1207 CIFS_MOUNT_MAP_SPECIAL_CHR);
1208 else if (attrs->ia_valid & ATTR_MODE) {
1209 if ((mode & S_IWUGO) == 0) /* not writeable */ {
1210 if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0)
1211 time_buf.Attributes =
1212 cpu_to_le32(cifsInode->cifsAttrs |
1214 } else if ((mode & S_IWUGO) == S_IWUGO) {
1215 if (cifsInode->cifsAttrs & ATTR_READONLY)
1216 time_buf.Attributes =
1217 cpu_to_le32(cifsInode->cifsAttrs &
1220 /* BB to be implemented -
1221 via Windows security descriptors or streams */
1222 /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid,
1223 cifs_sb->local_nls); */
1226 if (attrs->ia_valid & ATTR_ATIME) {
1228 time_buf.LastAccessTime =
1229 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1231 time_buf.LastAccessTime = 0;
1233 if (attrs->ia_valid & ATTR_MTIME) {
1235 time_buf.LastWriteTime =
1236 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1238 time_buf.LastWriteTime = 0;
1239 /* Do not set ctime explicitly unless other time
1240 stamps are changed explicitly (i.e. by utime()
1241 since we would then have a mix of client and
1244 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1246 /* Although Samba throws this field away
1247 it may be useful to Windows - but we do
1248 not want to set ctime unless some other
1249 timestamp is changing */
1250 cFYI(1, ("CIFS - CTIME changed "));
1251 time_buf.ChangeTime =
1252 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1254 time_buf.ChangeTime = 0;
1256 if (set_time || time_buf.Attributes) {
1257 time_buf.CreationTime = 0; /* do not change */
1258 /* In the future we should experiment - try setting timestamps
1259 via Handle (SetFileInfo) instead of by path */
1260 if (!(pTcon->ses->flags & CIFS_SES_NT4))
1261 rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
1263 cifs_sb->mnt_cifs_flags &
1264 CIFS_MOUNT_MAP_SPECIAL_CHR);
1268 if (rc == -EOPNOTSUPP) {
1272 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
1273 "times not supported by this server"));
1274 /* BB we could scan to see if we already have it open
1275 and pass in pid of opener to function */
1276 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1277 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1278 CREATE_NOT_DIR, &netfid, &oplock,
1279 NULL, cifs_sb->local_nls,
1280 cifs_sb->mnt_cifs_flags &
1281 CIFS_MOUNT_MAP_SPECIAL_CHR);
1283 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1285 CIFSSMBClose(xid, pTcon, netfid);
1287 /* BB For even older servers we could convert time_buf
1288 into old DOS style which uses two second
1291 /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
1292 &time_buf, cifs_sb->local_nls); */
1295 /* Even if error on time set, no sense failing the call if
1296 the server would set the time to a reasonable value anyway,
1297 and this check ensures that we are not being called from
1298 sys_utimes in which case we ought to fail the call back to
1299 the user when the server rejects the call */
1300 if((rc) && (attrs->ia_valid &&
1301 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
1305 /* do not need local check to inode_check_ok since the server does
1308 rc = inode_setattr(direntry->d_inode, attrs);
1315 void cifs_delete_inode(struct inode *inode)
1317 cFYI(1, ("In cifs_delete_inode, inode = 0x%p ", inode));
1318 /* may have to add back in if and when safe distributed caching of
1319 directories added e.g. via FindNotify */