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 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
168 &cifs_file_direct_nobrl_ops;
170 inode->i_fop = &cifs_file_direct_ops;
171 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
172 inode->i_fop = &cifs_file_nobrl_ops;
173 else /* not direct, send byte range locks */
174 inode->i_fop = &cifs_file_ops;
176 inode->i_data.a_ops = &cifs_addr_ops;
177 /* check if server can support readpages */
178 if(pTcon->ses->server->maxBuf <
179 4096 + MAX_CIFS_HDR_SIZE)
180 inode->i_data.a_ops->readpages = NULL;
181 } else if (S_ISDIR(inode->i_mode)) {
182 cFYI(1, ("Directory inode"));
183 inode->i_op = &cifs_dir_inode_ops;
184 inode->i_fop = &cifs_dir_ops;
185 } else if (S_ISLNK(inode->i_mode)) {
186 cFYI(1, ("Symbolic Link inode"));
187 inode->i_op = &cifs_symlink_inode_ops;
188 /* tmp_inode->i_fop = */ /* do not need to set to anything */
190 cFYI(1, ("Init special inode"));
191 init_special_inode(inode, inode->i_mode,
198 static int decode_sfu_inode(struct inode * inode, __u64 size,
199 const unsigned char *path,
200 struct cifs_sb_info *cifs_sb, int xid)
205 struct cifsTconInfo *pTcon = cifs_sb->tcon;
207 unsigned int bytes_read;
213 inode->i_mode |= S_IFIFO;
215 } else if (size < 8) {
216 return -EINVAL; /* EOPNOTSUPP? */
219 rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
220 CREATE_NOT_DIR, &netfid, &oplock, NULL,
222 cifs_sb->mnt_cifs_flags &
223 CIFS_MOUNT_MAP_SPECIAL_CHR);
226 rc = CIFSSMBRead(xid, pTcon,
228 8 /* length */, 0 /* offset */,
230 if((rc == 0) && (bytes_read == 8)) {
231 cERROR(1,("intx %s" ,pbuf));
232 if(memcmp("IntxBLK", pbuf, 8) == 0) {
233 cFYI(1,("Block device"));
234 inode->i_mode = S_IFBLK;
235 } else if(memcmp("IntxCHR", pbuf, 8) == 0) {
236 cFYI(1,("Char device"));
237 inode->i_mode = S_IFCHR;
238 } else if(memcmp("IntxLNK", pbuf, 7) == 0) {
240 inode->i_mode = S_IFLNK;
242 inode->i_mode = S_IFREG; /* then it is a file */
243 rc = -EOPNOTSUPP; /* or some unknown SFU type */
247 CIFSSMBClose(xid, pTcon, netfid);
250 /* inode->i_rdev = MKDEV(le64_to_cpu(DevMajor),
251 le64_to_cpu(DevMinor) & MINORMASK);*/
252 /* inode->i_mode |= S_IFBLK; */
258 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
260 static int get_sfu_uid_mode(struct inode * inode,
261 const unsigned char *path,
262 struct cifs_sb_info *cifs_sb, int xid)
268 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
269 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
270 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
274 mode = le32_to_cpu(*((__le32 *)ea_value));
275 inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
276 cFYI(1,("special mode bits 0%o", mode));
284 int cifs_get_inode_info(struct inode **pinode,
285 const unsigned char *search_path, FILE_ALL_INFO *pfindData,
286 struct super_block *sb, int xid)
289 struct cifsTconInfo *pTcon;
291 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
295 pTcon = cifs_sb->tcon;
296 cFYI(1,("Getting info on %s", search_path));
298 if ((pfindData == NULL) && (*pinode != NULL)) {
299 if (CIFS_I(*pinode)->clientCanCacheRead) {
300 cFYI(1,("No need to revalidate cached inode sizes"));
305 /* if file info not passed in then get it from server */
306 if (pfindData == NULL) {
307 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
310 pfindData = (FILE_ALL_INFO *)buf;
311 /* could do find first instead but this returns more info */
312 rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
313 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
314 CIFS_MOUNT_MAP_SPECIAL_CHR);
315 /* BB optimize code so we do not make the above call
316 when server claims no NT SMB support and the above call
317 failed at least once - set flag in tcon or mount */
318 if((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
319 rc = SMBQueryInformation(xid, pTcon, search_path,
320 pfindData, cifs_sb->local_nls,
321 cifs_sb->mnt_cifs_flags &
322 CIFS_MOUNT_MAP_SPECIAL_CHR);
326 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
328 if (rc == -EREMOTE) {
333 strnlen(search_path, MAX_PATHCONF) + 1,
335 if (tmp_path == NULL) {
340 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
341 strncat(tmp_path, search_path, MAX_PATHCONF);
342 rc = connect_to_dfs_path(xid, pTcon->ses,
343 /* treename + */ tmp_path,
345 cifs_sb->mnt_cifs_flags &
346 CIFS_MOUNT_MAP_SPECIAL_CHR);
348 /* BB fix up inode etc. */
354 struct cifsInodeInfo *cifsInfo;
355 __u32 attr = le32_to_cpu(pfindData->Attributes);
358 if (*pinode == NULL) {
359 *pinode = new_inode(sb);
362 /* Is an i_ino of zero legal? Can we use that to check
363 if the server supports returning inode numbers? Are
364 there other sanity checks we can use to ensure that
365 the server is really filling in that field? */
367 /* We can not use the IndexNumber field by default from
368 Windows or Samba (in ALL_INFO buf) but we can request
369 it explicitly. It may not be unique presumably if
370 the server has multiple devices mounted under one
373 /* There may be higher info levels that work but are
374 there Windows server or network appliances for which
375 IndexNumber field is not guaranteed unique? */
377 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM){
381 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
382 search_path, &inode_num,
384 cifs_sb->mnt_cifs_flags &
385 CIFS_MOUNT_MAP_SPECIAL_CHR);
387 cFYI(1,("GetSrvInodeNum rc %d", rc1));
388 /* BB EOPNOSUPP disable SERVER_INUM? */
389 } else /* do we need cast or hash to ino? */
390 (*pinode)->i_ino = inode_num;
391 } /* else ino incremented to unique num in new_inode*/
392 insert_inode_hash(*pinode);
395 cifsInfo = CIFS_I(inode);
396 cifsInfo->cifsAttrs = attr;
397 cFYI(1, (" Old time %ld ", cifsInfo->time));
398 cifsInfo->time = jiffies;
399 cFYI(1, (" New time %ld ", cifsInfo->time));
401 /* blksize needs to be multiple of two. So safer to default to
402 blksize and blkbits set in superblock so 2**blkbits and blksize
403 will match rather than setting to:
404 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
406 /* Linux can not store file creation time unfortunately so we ignore it */
408 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
410 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
412 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
413 cFYI(0, (" Attributes came in as 0x%x ", attr));
415 /* set default mode. will override for dirs below */
416 if (atomic_read(&cifsInfo->inUse) == 0)
417 /* new inode, can safely set these fields */
418 inode->i_mode = cifs_sb->mnt_file_mode;
420 /* if (attr & ATTR_REPARSE) */
421 /* We no longer handle these as symlinks because we could not
422 follow them due to the absolute path with drive letter */
423 if (attr & ATTR_DIRECTORY) {
424 /* override default perms since we do not do byte range locking
426 inode->i_mode = cifs_sb->mnt_dir_mode;
427 inode->i_mode |= S_IFDIR;
428 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
429 (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
430 /* No need to le64 convert size of zero */
431 (pfindData->EndOfFile == 0)) {
432 inode->i_mode = cifs_sb->mnt_file_mode;
433 inode->i_mode |= S_IFIFO;
434 /* BB Finish for SFU style symlinks and devices */
435 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
436 (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
437 if (decode_sfu_inode(inode,
438 le64_to_cpu(pfindData->EndOfFile),
441 cFYI(1,("Unrecognized sfu inode type"));
444 inode->i_mode |= S_IFREG;
445 /* treat the dos attribute of read-only as read-only
447 if (cifsInfo->cifsAttrs & ATTR_READONLY)
448 inode->i_mode &= ~(S_IWUGO);
449 /* BB add code here -
450 validate if device or weird share or device type? */
452 if (is_size_safe_to_change(cifsInfo)) {
453 /* can not safely change the file size here if the
454 client is writing to it due to potential races */
455 i_size_write(inode,le64_to_cpu(pfindData->EndOfFile));
457 /* 512 bytes (2**9) is the fake blocksize that must be
458 used for this calculation */
459 inode->i_blocks = (512 - 1 + le64_to_cpu(
460 pfindData->AllocationSize)) >> 9;
463 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
465 /* BB fill in uid and gid here? with help from winbind?
466 or retrieve from NTFS stream extended attribute */
467 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
468 /* fill in uid, gid, mode from server ACL */
469 get_sfu_uid_mode(inode, search_path, cifs_sb, xid);
470 } else if (atomic_read(&cifsInfo->inUse) == 0) {
471 inode->i_uid = cifs_sb->mnt_uid;
472 inode->i_gid = cifs_sb->mnt_gid;
473 /* set so we do not keep refreshing these fields with
474 bad data after user has changed them in memory */
475 atomic_set(&cifsInfo->inUse,1);
478 if (S_ISREG(inode->i_mode)) {
479 cFYI(1, ("File inode"));
480 inode->i_op = &cifs_file_inode_ops;
481 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
482 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
484 &cifs_file_direct_nobrl_ops;
486 inode->i_fop = &cifs_file_direct_ops;
487 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
488 inode->i_fop = &cifs_file_nobrl_ops;
489 else /* not direct, send byte range locks */
490 inode->i_fop = &cifs_file_ops;
492 inode->i_data.a_ops = &cifs_addr_ops;
493 if(pTcon->ses->server->maxBuf <
494 4096 + MAX_CIFS_HDR_SIZE)
495 inode->i_data.a_ops->readpages = NULL;
496 } else if (S_ISDIR(inode->i_mode)) {
497 cFYI(1, ("Directory inode"));
498 inode->i_op = &cifs_dir_inode_ops;
499 inode->i_fop = &cifs_dir_ops;
500 } else if (S_ISLNK(inode->i_mode)) {
501 cFYI(1, ("Symbolic Link inode"));
502 inode->i_op = &cifs_symlink_inode_ops;
504 init_special_inode(inode, inode->i_mode,
512 /* gets root inode */
513 void cifs_read_inode(struct inode *inode)
516 struct cifs_sb_info *cifs_sb;
518 cifs_sb = CIFS_SB(inode->i_sb);
520 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
521 cifs_get_inode_info_unix(&inode, "", inode->i_sb,xid);
523 cifs_get_inode_info(&inode, "", NULL, inode->i_sb,xid);
524 /* can not call macro FreeXid here since in a void func */
528 int cifs_unlink(struct inode *inode, struct dentry *direntry)
532 struct cifs_sb_info *cifs_sb;
533 struct cifsTconInfo *pTcon;
534 char *full_path = NULL;
535 struct cifsInodeInfo *cifsInode;
536 FILE_BASIC_INFO *pinfo_buf;
538 cFYI(1, (" cifs_unlink, inode = 0x%p with ", inode));
542 cifs_sb = CIFS_SB(inode->i_sb);
543 pTcon = cifs_sb->tcon;
545 /* Unlink can be called from rename so we can not grab the sem here
546 since we deadlock otherwise */
547 /* down(&direntry->d_sb->s_vfs_rename_sem);*/
548 full_path = build_path_from_dentry(direntry);
549 /* up(&direntry->d_sb->s_vfs_rename_sem);*/
550 if (full_path == NULL) {
554 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
555 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
558 if (direntry->d_inode)
559 direntry->d_inode->i_nlink--;
560 } else if (rc == -ENOENT) {
562 } else if (rc == -ETXTBSY) {
566 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
567 CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
568 &netfid, &oplock, NULL, cifs_sb->local_nls,
569 cifs_sb->mnt_cifs_flags &
570 CIFS_MOUNT_MAP_SPECIAL_CHR);
572 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
574 cifs_sb->mnt_cifs_flags &
575 CIFS_MOUNT_MAP_SPECIAL_CHR);
576 CIFSSMBClose(xid, pTcon, netfid);
577 if (direntry->d_inode)
578 direntry->d_inode->i_nlink--;
580 } else if (rc == -EACCES) {
581 /* try only if r/o attribute set in local lookup data? */
582 pinfo_buf = kmalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
584 memset(pinfo_buf, 0, sizeof(FILE_BASIC_INFO));
585 /* ATTRS set to normal clears r/o bit */
586 pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
587 if (!(pTcon->ses->flags & CIFS_SES_NT4))
588 rc = CIFSSMBSetTimes(xid, pTcon, full_path,
591 cifs_sb->mnt_cifs_flags &
592 CIFS_MOUNT_MAP_SPECIAL_CHR);
596 if (rc == -EOPNOTSUPP) {
599 /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
603 For some strange reason it seems that NT4 eats the
604 old setattr call without actually setting the
605 attributes so on to the third attempted workaround
608 /* BB could scan to see if we already have it open
609 and pass in pid of opener to function */
610 rc = CIFSSMBOpen(xid, pTcon, full_path,
611 FILE_OPEN, SYNCHRONIZE |
612 FILE_WRITE_ATTRIBUTES, 0,
613 &netfid, &oplock, NULL,
615 cifs_sb->mnt_cifs_flags &
616 CIFS_MOUNT_MAP_SPECIAL_CHR);
618 rc = CIFSSMBSetFileTimes(xid, pTcon,
621 CIFSSMBClose(xid, pTcon, netfid);
627 rc = CIFSSMBDelFile(xid, pTcon, full_path,
629 cifs_sb->mnt_cifs_flags &
630 CIFS_MOUNT_MAP_SPECIAL_CHR);
632 if (direntry->d_inode)
633 direntry->d_inode->i_nlink--;
634 } else if (rc == -ETXTBSY) {
638 rc = CIFSSMBOpen(xid, pTcon, full_path,
641 CREATE_DELETE_ON_CLOSE,
642 &netfid, &oplock, NULL,
644 cifs_sb->mnt_cifs_flags &
645 CIFS_MOUNT_MAP_SPECIAL_CHR);
647 CIFSSMBRenameOpenFile(xid, pTcon,
650 cifs_sb->mnt_cifs_flags &
651 CIFS_MOUNT_MAP_SPECIAL_CHR);
652 CIFSSMBClose(xid, pTcon, netfid);
653 if (direntry->d_inode)
654 direntry->d_inode->i_nlink--;
656 /* BB if rc = -ETXTBUSY goto the rename logic BB */
660 if (direntry->d_inode) {
661 cifsInode = CIFS_I(direntry->d_inode);
662 cifsInode->time = 0; /* will force revalidate to get info
664 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
666 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
667 cifsInode = CIFS_I(inode);
668 cifsInode->time = 0; /* force revalidate of dir as well */
675 int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
679 struct cifs_sb_info *cifs_sb;
680 struct cifsTconInfo *pTcon;
681 char *full_path = NULL;
682 struct inode *newinode = NULL;
684 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p ", mode, inode));
688 cifs_sb = CIFS_SB(inode->i_sb);
689 pTcon = cifs_sb->tcon;
691 down(&inode->i_sb->s_vfs_rename_sem);
692 full_path = build_path_from_dentry(direntry);
693 up(&inode->i_sb->s_vfs_rename_sem);
694 if (full_path == NULL) {
698 /* BB add setting the equivalent of mode via CreateX w/ACLs */
699 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
700 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
702 cFYI(1, ("cifs_mkdir returned 0x%x ", rc));
706 if (pTcon->ses->capabilities & CAP_UNIX)
707 rc = cifs_get_inode_info_unix(&newinode, full_path,
710 rc = cifs_get_inode_info(&newinode, full_path, NULL,
714 direntry->d_op = &cifs_ci_dentry_ops;
716 direntry->d_op = &cifs_dentry_ops;
717 d_instantiate(direntry, newinode);
718 if (direntry->d_inode)
719 direntry->d_inode->i_nlink = 2;
720 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
721 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
722 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
724 (__u64)current->euid,
725 (__u64)current->egid,
728 cifs_sb->mnt_cifs_flags &
729 CIFS_MOUNT_MAP_SPECIAL_CHR);
731 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
733 (__u64)-1, 0 /* dev_t */,
735 cifs_sb->mnt_cifs_flags &
736 CIFS_MOUNT_MAP_SPECIAL_CHR);
739 /* BB to be implemented via Windows secrty descriptors
740 eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
741 -1, -1, local_nls); */
749 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
753 struct cifs_sb_info *cifs_sb;
754 struct cifsTconInfo *pTcon;
755 char *full_path = NULL;
756 struct cifsInodeInfo *cifsInode;
758 cFYI(1, (" cifs_rmdir, inode = 0x%p with ", inode));
762 cifs_sb = CIFS_SB(inode->i_sb);
763 pTcon = cifs_sb->tcon;
765 down(&inode->i_sb->s_vfs_rename_sem);
766 full_path = build_path_from_dentry(direntry);
767 up(&inode->i_sb->s_vfs_rename_sem);
768 if (full_path == NULL) {
773 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
774 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
778 i_size_write(direntry->d_inode,0);
779 direntry->d_inode->i_nlink = 0;
782 cifsInode = CIFS_I(direntry->d_inode);
783 cifsInode->time = 0; /* force revalidate to go get info when
785 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
786 current_fs_time(inode->i_sb);
793 int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
794 struct inode *target_inode, struct dentry *target_direntry)
798 struct cifs_sb_info *cifs_sb_source;
799 struct cifs_sb_info *cifs_sb_target;
800 struct cifsTconInfo *pTcon;
806 cifs_sb_target = CIFS_SB(target_inode->i_sb);
807 cifs_sb_source = CIFS_SB(source_inode->i_sb);
808 pTcon = cifs_sb_source->tcon;
810 if (pTcon != cifs_sb_target->tcon) {
812 return -EXDEV; /* BB actually could be allowed if same server,
814 Might eventually add support for this */
817 /* we already have the rename sem so we do not need to grab it again
818 here to protect the path integrity */
819 fromName = build_path_from_dentry(source_direntry);
820 toName = build_path_from_dentry(target_direntry);
821 if ((fromName == NULL) || (toName == NULL)) {
823 goto cifs_rename_exit;
826 rc = CIFSSMBRename(xid, pTcon, fromName, toName,
827 cifs_sb_source->local_nls,
828 cifs_sb_source->mnt_cifs_flags &
829 CIFS_MOUNT_MAP_SPECIAL_CHR);
831 /* check if they are the same file because rename of hardlinked
833 FILE_UNIX_BASIC_INFO *info_buf_source;
834 FILE_UNIX_BASIC_INFO *info_buf_target;
837 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
838 if (info_buf_source != NULL) {
839 info_buf_target = info_buf_source + 1;
840 rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
841 info_buf_source, cifs_sb_source->local_nls,
842 cifs_sb_source->mnt_cifs_flags &
843 CIFS_MOUNT_MAP_SPECIAL_CHR);
845 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
847 cifs_sb_target->local_nls,
848 /* remap based on source sb */
849 cifs_sb_source->mnt_cifs_flags &
850 CIFS_MOUNT_MAP_SPECIAL_CHR);
853 (info_buf_source->UniqueId ==
854 info_buf_target->UniqueId)) {
855 /* do not rename since the files are hardlinked which
858 /* we either can not tell the files are hardlinked
859 (as with Windows servers) or files are not
860 hardlinked so delete the target manually before
861 renaming to follow POSIX rather than Windows
863 cifs_unlink(target_inode, target_direntry);
864 rc = CIFSSMBRename(xid, pTcon, fromName,
866 cifs_sb_source->local_nls,
867 cifs_sb_source->mnt_cifs_flags
868 & CIFS_MOUNT_MAP_SPECIAL_CHR);
870 kfree(info_buf_source);
871 } /* if we can not get memory just leave rc as EEXIST */
875 cFYI(1, ("rename rc %d", rc));
878 if ((rc == -EIO) || (rc == -EEXIST)) {
882 /* BB FIXME Is Generic Read correct for rename? */
883 /* if renaming directory - we should not say CREATE_NOT_DIR,
884 need to test renaming open directory, also GENERIC_READ
885 might not right be right access to request */
886 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
887 CREATE_NOT_DIR, &netfid, &oplock, NULL,
888 cifs_sb_source->local_nls,
889 cifs_sb_source->mnt_cifs_flags &
890 CIFS_MOUNT_MAP_SPECIAL_CHR);
892 CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
893 cifs_sb_source->local_nls,
894 cifs_sb_source->mnt_cifs_flags &
895 CIFS_MOUNT_MAP_SPECIAL_CHR);
896 CIFSSMBClose(xid, pTcon, netfid);
907 int cifs_revalidate(struct dentry *direntry)
912 struct cifs_sb_info *cifs_sb;
913 struct cifsInodeInfo *cifsInode;
915 struct timespec local_mtime;
916 int invalidate_inode = FALSE;
918 if (direntry->d_inode == NULL)
921 cifsInode = CIFS_I(direntry->d_inode);
923 if (cifsInode == NULL)
926 /* no sense revalidating inode info on file that no one can write */
927 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
932 cifs_sb = CIFS_SB(direntry->d_sb);
934 /* can not safely grab the rename sem here if rename calls revalidate
935 since that would deadlock */
936 full_path = build_path_from_dentry(direntry);
937 if (full_path == NULL) {
941 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
942 "jiffies %ld", full_path, direntry->d_inode,
943 direntry->d_inode->i_count.counter, direntry,
944 direntry->d_time, jiffies));
946 if (cifsInode->time == 0) {
947 /* was set to zero previously to force revalidate */
948 } else if (time_before(jiffies, cifsInode->time + HZ) &&
949 lookupCacheEnabled) {
950 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
951 (direntry->d_inode->i_nlink == 1)) {
956 cFYI(1, ("Have to revalidate file due to hardlinks"));
960 /* save mtime and size */
961 local_mtime = direntry->d_inode->i_mtime;
962 local_size = direntry->d_inode->i_size;
964 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
965 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
968 cFYI(1, ("error on getting revalidate info %d", rc));
969 /* if (rc != -ENOENT)
970 rc = 0; */ /* BB should we cache info on
974 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
977 cFYI(1, ("error on getting revalidate info %d", rc));
978 /* if (rc != -ENOENT)
979 rc = 0; */ /* BB should we cache info on
983 /* should we remap certain errors, access denied?, to zero */
985 /* if not oplocked, we invalidate inode pages if mtime or file size
986 had changed on server */
988 if (timespec_equal(&local_mtime,&direntry->d_inode->i_mtime) &&
989 (local_size == direntry->d_inode->i_size)) {
990 cFYI(1, ("cifs_revalidate - inode unchanged"));
992 /* file may have changed on server */
993 if (cifsInode->clientCanCacheRead) {
994 /* no need to invalidate inode pages since we were the
995 only ones who could have modified the file and the
996 server copy is staler than ours */
998 invalidate_inode = TRUE;
1002 /* can not grab this sem since kernel filesys locking documentation
1003 indicates i_sem may be taken by the kernel on lookup and rename
1004 which could deadlock if we grab the i_sem here as well */
1005 /* down(&direntry->d_inode->i_sem);*/
1006 /* need to write out dirty pages here */
1007 if (direntry->d_inode->i_mapping) {
1008 /* do we need to lock inode until after invalidate completes
1010 filemap_fdatawrite(direntry->d_inode->i_mapping);
1012 if (invalidate_inode) {
1013 if (direntry->d_inode->i_mapping)
1014 filemap_fdatawait(direntry->d_inode->i_mapping);
1015 /* may eventually have to do this for open files too */
1016 if (list_empty(&(cifsInode->openFileList))) {
1017 /* Has changed on server - flush read ahead pages */
1018 cFYI(1, ("Invalidating read ahead data on "
1020 invalidate_remote_inode(direntry->d_inode);
1023 /* up(&direntry->d_inode->i_sem); */
1030 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1033 int err = cifs_revalidate(dentry);
1035 generic_fillattr(dentry->d_inode, stat);
1039 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1041 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1042 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1047 page = grab_cache_page(mapping, index);
1051 kaddr = kmap_atomic(page, KM_USER0);
1052 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1053 flush_dcache_page(page);
1054 kunmap_atomic(kaddr, KM_USER0);
1056 page_cache_release(page);
1060 int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
1063 struct cifs_sb_info *cifs_sb;
1064 struct cifsTconInfo *pTcon;
1065 char *full_path = NULL;
1067 struct cifsFileInfo *open_file = NULL;
1068 FILE_BASIC_INFO time_buf;
1069 int set_time = FALSE;
1070 __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
1071 __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
1072 __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
1073 struct cifsInodeInfo *cifsInode;
1077 cFYI(1, (" In cifs_setattr, name = %s attrs->iavalid 0x%x ",
1078 direntry->d_name.name, attrs->ia_valid));
1079 cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
1080 pTcon = cifs_sb->tcon;
1082 down(&direntry->d_sb->s_vfs_rename_sem);
1083 full_path = build_path_from_dentry(direntry);
1084 up(&direntry->d_sb->s_vfs_rename_sem);
1085 if (full_path == NULL) {
1089 cifsInode = CIFS_I(direntry->d_inode);
1091 /* BB check if we need to refresh inode from server now ? BB */
1093 /* need to flush data before changing file size on server */
1094 filemap_fdatawrite(direntry->d_inode->i_mapping);
1095 filemap_fdatawait(direntry->d_inode->i_mapping);
1097 if (attrs->ia_valid & ATTR_SIZE) {
1098 /* To avoid spurious oplock breaks from server, in the case of
1099 inodes that we already have open, avoid doing path based
1100 setting of file size if we can do it by handle.
1101 This keeps our caching token (oplock) and avoids timeouts
1102 when the local oplock break takes longer to flush
1103 writebehind data than the SMB timeout for the SetPathInfo
1104 request would allow */
1105 open_file = find_writable_file(cifsInode);
1107 __u16 nfid = open_file->netfid;
1108 __u32 npid = open_file->pid;
1109 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
1111 atomic_dec(&open_file->wrtPending);
1112 cFYI(1,("SetFSize for attrs rc = %d", rc));
1115 rc = CIFSSMBWrite(xid, pTcon,
1116 nfid, 0, attrs->ia_size,
1117 &bytes_written, NULL, NULL,
1118 1 /* 45 seconds */);
1119 cFYI(1,("Wrt seteof rc %d", rc));
1123 /* Set file size by pathname rather than by handle
1124 either because no valid, writeable file handle for
1125 it was found or because there was an error setting
1127 rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1128 attrs->ia_size, FALSE,
1130 cifs_sb->mnt_cifs_flags &
1131 CIFS_MOUNT_MAP_SPECIAL_CHR);
1132 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1137 rc = SMBLegacyOpen(xid, pTcon, full_path,
1139 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1140 CREATE_NOT_DIR, &netfid, &oplock,
1141 NULL, cifs_sb->local_nls,
1142 cifs_sb->mnt_cifs_flags &
1143 CIFS_MOUNT_MAP_SPECIAL_CHR);
1146 rc = CIFSSMBWrite(xid, pTcon,
1149 &bytes_written, NULL,
1150 NULL, 1 /* 45 sec */);
1151 cFYI(1,("wrt seteof rc %d",rc));
1152 CIFSSMBClose(xid, pTcon, netfid);
1158 /* Server is ok setting allocation size implicitly - no need
1160 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
1161 cifs_sb->local_nls);
1165 rc = vmtruncate(direntry->d_inode, attrs->ia_size);
1166 cifs_truncate_page(direntry->d_inode->i_mapping,
1167 direntry->d_inode->i_size);
1169 goto cifs_setattr_exit;
1171 if (attrs->ia_valid & ATTR_UID) {
1172 cFYI(1, ("UID changed to %d", attrs->ia_uid));
1173 uid = attrs->ia_uid;
1175 if (attrs->ia_valid & ATTR_GID) {
1176 cFYI(1, ("GID changed to %d", attrs->ia_gid));
1177 gid = attrs->ia_gid;
1180 time_buf.Attributes = 0;
1181 if (attrs->ia_valid & ATTR_MODE) {
1182 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
1183 mode = attrs->ia_mode;
1186 if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX)
1187 && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
1188 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
1189 0 /* dev_t */, cifs_sb->local_nls,
1190 cifs_sb->mnt_cifs_flags &
1191 CIFS_MOUNT_MAP_SPECIAL_CHR);
1192 else if (attrs->ia_valid & ATTR_MODE) {
1193 if ((mode & S_IWUGO) == 0) /* not writeable */ {
1194 if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0)
1195 time_buf.Attributes =
1196 cpu_to_le32(cifsInode->cifsAttrs |
1198 } else if ((mode & S_IWUGO) == S_IWUGO) {
1199 if (cifsInode->cifsAttrs & ATTR_READONLY)
1200 time_buf.Attributes =
1201 cpu_to_le32(cifsInode->cifsAttrs &
1204 /* BB to be implemented -
1205 via Windows security descriptors or streams */
1206 /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid,
1207 cifs_sb->local_nls); */
1210 if (attrs->ia_valid & ATTR_ATIME) {
1212 time_buf.LastAccessTime =
1213 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1215 time_buf.LastAccessTime = 0;
1217 if (attrs->ia_valid & ATTR_MTIME) {
1219 time_buf.LastWriteTime =
1220 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1222 time_buf.LastWriteTime = 0;
1223 /* Do not set ctime explicitly unless other time
1224 stamps are changed explicitly (i.e. by utime()
1225 since we would then have a mix of client and
1228 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1230 /* Although Samba throws this field away
1231 it may be useful to Windows - but we do
1232 not want to set ctime unless some other
1233 timestamp is changing */
1234 cFYI(1, ("CIFS - CTIME changed "));
1235 time_buf.ChangeTime =
1236 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1238 time_buf.ChangeTime = 0;
1240 if (set_time || time_buf.Attributes) {
1241 time_buf.CreationTime = 0; /* do not change */
1242 /* In the future we should experiment - try setting timestamps
1243 via Handle (SetFileInfo) instead of by path */
1244 if (!(pTcon->ses->flags & CIFS_SES_NT4))
1245 rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
1247 cifs_sb->mnt_cifs_flags &
1248 CIFS_MOUNT_MAP_SPECIAL_CHR);
1252 if (rc == -EOPNOTSUPP) {
1256 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
1257 "times not supported by this server"));
1258 /* BB we could scan to see if we already have it open
1259 and pass in pid of opener to function */
1260 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1261 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1262 CREATE_NOT_DIR, &netfid, &oplock,
1263 NULL, cifs_sb->local_nls,
1264 cifs_sb->mnt_cifs_flags &
1265 CIFS_MOUNT_MAP_SPECIAL_CHR);
1267 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1269 CIFSSMBClose(xid, pTcon, netfid);
1271 /* BB For even older servers we could convert time_buf
1272 into old DOS style which uses two second
1275 /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
1276 &time_buf, cifs_sb->local_nls); */
1279 /* Even if error on time set, no sense failing the call if
1280 the server would set the time to a reasonable value anyway,
1281 and this check ensures that we are not being called from
1282 sys_utimes in which case we ought to fail the call back to
1283 the user when the server rejects the call */
1284 if((rc) && (attrs->ia_valid &&
1285 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
1289 /* do not need local check to inode_check_ok since the server does
1292 rc = inode_setattr(direntry->d_inode, attrs);
1299 void cifs_delete_inode(struct inode *inode)
1301 cFYI(1, ("In cifs_delete_inode, inode = 0x%p ", inode));
1302 /* may have to add back in if and when safe distributed caching of
1303 directories added e.g. via FindNotify */