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/stat.h>
23 #include <linux/pagemap.h>
24 #include <asm/div64.h>
28 #include "cifsproto.h"
29 #include "cifs_debug.h"
30 #include "cifs_fs_sb.h"
32 int cifs_get_inode_info_unix(struct inode **pinode,
33 const unsigned char *search_path, struct super_block *sb, int xid)
36 FILE_UNIX_BASIC_INFO findData;
37 struct cifsTconInfo *pTcon;
39 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
42 pTcon = cifs_sb->tcon;
43 cFYI(1, ("Getting info on %s", search_path));
44 /* could have done a find first instead but this returns more info */
45 rc = CIFSSMBUnixQPathInfo(xid, pTcon, search_path, &findData,
46 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
47 CIFS_MOUNT_MAP_SPECIAL_CHR);
48 /* dump_mem("\nUnixQPathInfo return data", &findData,
53 kmalloc(strnlen(pTcon->treeName,
55 strnlen(search_path, MAX_PATHCONF) + 1,
57 if (tmp_path == NULL) {
60 /* have to skip first of the double backslash of
62 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
63 strncat(tmp_path, search_path, MAX_PATHCONF);
64 rc = connect_to_dfs_path(xid, pTcon->ses,
65 /* treename + */ tmp_path,
67 cifs_sb->mnt_cifs_flags &
68 CIFS_MOUNT_MAP_SPECIAL_CHR);
71 /* BB fix up inode etc. */
76 struct cifsInodeInfo *cifsInfo;
77 __u32 type = le32_to_cpu(findData.Type);
78 __u64 num_of_bytes = le64_to_cpu(findData.NumOfBytes);
79 __u64 end_of_file = le64_to_cpu(findData.EndOfFile);
82 if (*pinode == NULL) {
83 *pinode = new_inode(sb);
86 /* Is an i_ino of zero legal? */
87 /* Are there sanity checks we can use to ensure that
88 the server is really filling in that field? */
89 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
91 (unsigned long)findData.UniqueId;
92 } /* note ino incremented to unique num in new_inode */
93 if(sb->s_flags & MS_NOATIME)
94 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
96 insert_inode_hash(*pinode);
100 cifsInfo = CIFS_I(inode);
102 cFYI(1, ("Old time %ld", cifsInfo->time));
103 cifsInfo->time = jiffies;
104 cFYI(1, ("New time %ld", cifsInfo->time));
105 /* this is ok to set on every inode revalidate */
106 atomic_set(&cifsInfo->inUse,1);
109 cifs_NTtimeToUnix(le64_to_cpu(findData.LastAccessTime));
111 cifs_NTtimeToUnix(le64_to_cpu
112 (findData.LastModificationTime));
114 cifs_NTtimeToUnix(le64_to_cpu(findData.LastStatusChange));
115 inode->i_mode = le64_to_cpu(findData.Permissions);
116 /* since we set the inode type below we need to mask off
117 to avoid strange results if bits set above */
118 inode->i_mode &= ~S_IFMT;
119 if (type == UNIX_FILE) {
120 inode->i_mode |= S_IFREG;
121 } else if (type == UNIX_SYMLINK) {
122 inode->i_mode |= S_IFLNK;
123 } else if (type == UNIX_DIR) {
124 inode->i_mode |= S_IFDIR;
125 } else if (type == UNIX_CHARDEV) {
126 inode->i_mode |= S_IFCHR;
127 inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
128 le64_to_cpu(findData.DevMinor) & MINORMASK);
129 } else if (type == UNIX_BLOCKDEV) {
130 inode->i_mode |= S_IFBLK;
131 inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
132 le64_to_cpu(findData.DevMinor) & MINORMASK);
133 } else if (type == UNIX_FIFO) {
134 inode->i_mode |= S_IFIFO;
135 } else if (type == UNIX_SOCKET) {
136 inode->i_mode |= S_IFSOCK;
138 /* safest to call it a file if we do not know */
139 inode->i_mode |= S_IFREG;
140 cFYI(1,("unknown type %d",type));
142 inode->i_uid = le64_to_cpu(findData.Uid);
143 inode->i_gid = le64_to_cpu(findData.Gid);
144 inode->i_nlink = le64_to_cpu(findData.Nlinks);
146 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
147 /* can not safely change the file size here if the
148 client is writing to it due to potential races */
150 i_size_write(inode, end_of_file);
152 /* blksize needs to be multiple of two. So safer to default to
153 blksize and blkbits set in superblock so 2**blkbits and blksize
154 will match rather than setting to:
155 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
157 /* This seems incredibly stupid but it turns out that i_blocks
158 is not related to (i_size / i_blksize), instead 512 byte size
159 is required for calculating num blocks */
161 /* 512 bytes (2**9) is the fake blocksize that must be used */
162 /* for this calculation */
163 inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
166 if (num_of_bytes < end_of_file)
167 cFYI(1, ("allocation size less than end of file"));
168 cFYI(1, ("Size %ld and blocks %llu",
169 (unsigned long) inode->i_size,
170 (unsigned long long)inode->i_blocks));
171 if (S_ISREG(inode->i_mode)) {
172 cFYI(1, ("File inode"));
173 inode->i_op = &cifs_file_inode_ops;
174 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
175 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
177 &cifs_file_direct_nobrl_ops;
179 inode->i_fop = &cifs_file_direct_ops;
180 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
181 inode->i_fop = &cifs_file_nobrl_ops;
182 else /* not direct, send byte range locks */
183 inode->i_fop = &cifs_file_ops;
185 /* check if server can support readpages */
186 if(pTcon->ses->server->maxBuf <
187 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
188 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
190 inode->i_data.a_ops = &cifs_addr_ops;
191 } else if (S_ISDIR(inode->i_mode)) {
192 cFYI(1, ("Directory inode"));
193 inode->i_op = &cifs_dir_inode_ops;
194 inode->i_fop = &cifs_dir_ops;
195 } else if (S_ISLNK(inode->i_mode)) {
196 cFYI(1, ("Symbolic Link inode"));
197 inode->i_op = &cifs_symlink_inode_ops;
198 /* tmp_inode->i_fop = */ /* do not need to set to anything */
200 cFYI(1, ("Init special inode"));
201 init_special_inode(inode, inode->i_mode,
208 static int decode_sfu_inode(struct inode * inode, __u64 size,
209 const unsigned char *path,
210 struct cifs_sb_info *cifs_sb, int xid)
215 struct cifsTconInfo *pTcon = cifs_sb->tcon;
217 unsigned int bytes_read;
223 inode->i_mode |= S_IFIFO;
225 } else if (size < 8) {
226 return -EINVAL; /* EOPNOTSUPP? */
229 rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
230 CREATE_NOT_DIR, &netfid, &oplock, NULL,
232 cifs_sb->mnt_cifs_flags &
233 CIFS_MOUNT_MAP_SPECIAL_CHR);
235 int buf_type = CIFS_NO_BUFFER;
237 rc = CIFSSMBRead(xid, pTcon,
239 24 /* length */, 0 /* offset */,
240 &bytes_read, &pbuf, &buf_type);
241 if((rc == 0) && (bytes_read >= 8)) {
242 if(memcmp("IntxBLK", pbuf, 8) == 0) {
243 cFYI(1,("Block device"));
244 inode->i_mode |= S_IFBLK;
245 if(bytes_read == 24) {
246 /* we have enough to decode dev num */
247 __u64 mjr; /* major */
248 __u64 mnr; /* minor */
249 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
250 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
251 inode->i_rdev = MKDEV(mjr, mnr);
253 } else if(memcmp("IntxCHR", pbuf, 8) == 0) {
254 cFYI(1,("Char device"));
255 inode->i_mode |= S_IFCHR;
256 if(bytes_read == 24) {
257 /* we have enough to decode dev num */
258 __u64 mjr; /* major */
259 __u64 mnr; /* minor */
260 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
261 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
262 inode->i_rdev = MKDEV(mjr, mnr);
264 } else if(memcmp("IntxLNK", pbuf, 7) == 0) {
266 inode->i_mode |= S_IFLNK;
268 inode->i_mode |= S_IFREG; /* file? */
272 inode->i_mode |= S_IFREG; /* then it is a file */
273 rc = -EOPNOTSUPP; /* or some unknown SFU type */
275 CIFSSMBClose(xid, pTcon, netfid);
281 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
283 static int get_sfu_uid_mode(struct inode * inode,
284 const unsigned char *path,
285 struct cifs_sb_info *cifs_sb, int xid)
287 #ifdef CONFIG_CIFS_XATTR
292 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
293 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
294 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
298 mode = le32_to_cpu(*((__le32 *)ea_value));
299 inode->i_mode &= ~SFBITS_MASK;
300 cFYI(1,("special bits 0%o org mode 0%o", mode, inode->i_mode));
301 inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
302 cFYI(1,("special mode bits 0%o", mode));
314 int cifs_get_inode_info(struct inode **pinode,
315 const unsigned char *search_path, FILE_ALL_INFO *pfindData,
316 struct super_block *sb, int xid)
319 struct cifsTconInfo *pTcon;
321 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
324 int adjustTZ = FALSE;
326 pTcon = cifs_sb->tcon;
327 cFYI(1,("Getting info on %s", search_path));
329 if ((pfindData == NULL) && (*pinode != NULL)) {
330 if (CIFS_I(*pinode)->clientCanCacheRead) {
331 cFYI(1,("No need to revalidate cached inode sizes"));
336 /* if file info not passed in then get it from server */
337 if (pfindData == NULL) {
338 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
341 pfindData = (FILE_ALL_INFO *)buf;
342 /* could do find first instead but this returns more info */
343 rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
345 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
346 CIFS_MOUNT_MAP_SPECIAL_CHR);
347 /* BB optimize code so we do not make the above call
348 when server claims no NT SMB support and the above call
349 failed at least once - set flag in tcon or mount */
350 if((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
351 rc = SMBQueryInformation(xid, pTcon, search_path,
352 pfindData, cifs_sb->local_nls,
353 cifs_sb->mnt_cifs_flags &
354 CIFS_MOUNT_MAP_SPECIAL_CHR);
359 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
361 if (rc == -EREMOTE) {
366 strnlen(search_path, MAX_PATHCONF) + 1,
368 if (tmp_path == NULL) {
373 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
374 strncat(tmp_path, search_path, MAX_PATHCONF);
375 rc = connect_to_dfs_path(xid, pTcon->ses,
376 /* treename + */ tmp_path,
378 cifs_sb->mnt_cifs_flags &
379 CIFS_MOUNT_MAP_SPECIAL_CHR);
381 /* BB fix up inode etc. */
387 struct cifsInodeInfo *cifsInfo;
388 __u32 attr = le32_to_cpu(pfindData->Attributes);
391 if (*pinode == NULL) {
392 *pinode = new_inode(sb);
393 if (*pinode == NULL) {
397 /* Is an i_ino of zero legal? Can we use that to check
398 if the server supports returning inode numbers? Are
399 there other sanity checks we can use to ensure that
400 the server is really filling in that field? */
402 /* We can not use the IndexNumber field by default from
403 Windows or Samba (in ALL_INFO buf) but we can request
404 it explicitly. It may not be unique presumably if
405 the server has multiple devices mounted under one
408 /* There may be higher info levels that work but are
409 there Windows server or network appliances for which
410 IndexNumber field is not guaranteed unique? */
412 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM){
416 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
417 search_path, &inode_num,
419 cifs_sb->mnt_cifs_flags &
420 CIFS_MOUNT_MAP_SPECIAL_CHR);
422 cFYI(1,("GetSrvInodeNum rc %d", rc1));
423 /* BB EOPNOSUPP disable SERVER_INUM? */
424 } else /* do we need cast or hash to ino? */
425 (*pinode)->i_ino = inode_num;
426 } /* else ino incremented to unique num in new_inode*/
427 if(sb->s_flags & MS_NOATIME)
428 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
429 insert_inode_hash(*pinode);
432 cifsInfo = CIFS_I(inode);
433 cifsInfo->cifsAttrs = attr;
434 cFYI(1, ("Old time %ld", cifsInfo->time));
435 cifsInfo->time = jiffies;
436 cFYI(1, ("New time %ld", cifsInfo->time));
438 /* blksize needs to be multiple of two. So safer to default to
439 blksize and blkbits set in superblock so 2**blkbits and blksize
440 will match rather than setting to:
441 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
443 /* Linux can not store file creation time so ignore it */
444 if(pfindData->LastAccessTime)
445 inode->i_atime = cifs_NTtimeToUnix
446 (le64_to_cpu(pfindData->LastAccessTime));
447 else /* do not need to use current_fs_time - time not stored */
448 inode->i_atime = CURRENT_TIME;
450 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
452 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
453 cFYI(0, ("Attributes came in as 0x%x", attr));
454 if(adjustTZ && (pTcon->ses) && (pTcon->ses->server)) {
455 inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj;
456 inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj;
459 /* set default mode. will override for dirs below */
460 if (atomic_read(&cifsInfo->inUse) == 0)
461 /* new inode, can safely set these fields */
462 inode->i_mode = cifs_sb->mnt_file_mode;
463 else /* since we set the inode type below we need to mask off
464 to avoid strange results if type changes and both get orred in */
465 inode->i_mode &= ~S_IFMT;
466 /* if (attr & ATTR_REPARSE) */
467 /* We no longer handle these as symlinks because we could not
468 follow them due to the absolute path with drive letter */
469 if (attr & ATTR_DIRECTORY) {
470 /* override default perms since we do not do byte range locking
472 inode->i_mode = cifs_sb->mnt_dir_mode;
473 inode->i_mode |= S_IFDIR;
474 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
475 (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
476 /* No need to le64 convert size of zero */
477 (pfindData->EndOfFile == 0)) {
478 inode->i_mode = cifs_sb->mnt_file_mode;
479 inode->i_mode |= S_IFIFO;
480 /* BB Finish for SFU style symlinks and devices */
481 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
482 (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
483 if (decode_sfu_inode(inode,
484 le64_to_cpu(pfindData->EndOfFile),
487 cFYI(1,("Unrecognized sfu inode type"));
489 cFYI(1,("sfu mode 0%o",inode->i_mode));
491 inode->i_mode |= S_IFREG;
492 /* treat the dos attribute of read-only as read-only
494 if (cifsInfo->cifsAttrs & ATTR_READONLY)
495 inode->i_mode &= ~(S_IWUGO);
496 /* BB add code here -
497 validate if device or weird share or device type? */
499 if (is_size_safe_to_change(cifsInfo, le64_to_cpu(pfindData->EndOfFile))) {
500 /* can not safely shrink the file size here if the
501 client is writing to it due to potential races */
502 i_size_write(inode,le64_to_cpu(pfindData->EndOfFile));
504 /* 512 bytes (2**9) is the fake blocksize that must be
505 used for this calculation */
506 inode->i_blocks = (512 - 1 + le64_to_cpu(
507 pfindData->AllocationSize)) >> 9;
510 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
512 /* BB fill in uid and gid here? with help from winbind?
513 or retrieve from NTFS stream extended attribute */
514 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
515 /* fill in uid, gid, mode from server ACL */
516 get_sfu_uid_mode(inode, search_path, cifs_sb, xid);
517 } else if (atomic_read(&cifsInfo->inUse) == 0) {
518 inode->i_uid = cifs_sb->mnt_uid;
519 inode->i_gid = cifs_sb->mnt_gid;
520 /* set so we do not keep refreshing these fields with
521 bad data after user has changed them in memory */
522 atomic_set(&cifsInfo->inUse,1);
525 if (S_ISREG(inode->i_mode)) {
526 cFYI(1, ("File inode"));
527 inode->i_op = &cifs_file_inode_ops;
528 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
529 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
531 &cifs_file_direct_nobrl_ops;
533 inode->i_fop = &cifs_file_direct_ops;
534 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
535 inode->i_fop = &cifs_file_nobrl_ops;
536 else /* not direct, send byte range locks */
537 inode->i_fop = &cifs_file_ops;
539 if(pTcon->ses->server->maxBuf <
540 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
541 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
543 inode->i_data.a_ops = &cifs_addr_ops;
544 } else if (S_ISDIR(inode->i_mode)) {
545 cFYI(1, ("Directory inode"));
546 inode->i_op = &cifs_dir_inode_ops;
547 inode->i_fop = &cifs_dir_ops;
548 } else if (S_ISLNK(inode->i_mode)) {
549 cFYI(1, ("Symbolic Link inode"));
550 inode->i_op = &cifs_symlink_inode_ops;
552 init_special_inode(inode, inode->i_mode,
560 /* gets root inode */
561 void cifs_read_inode(struct inode *inode)
564 struct cifs_sb_info *cifs_sb;
566 cifs_sb = CIFS_SB(inode->i_sb);
568 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
569 cifs_get_inode_info_unix(&inode, "", inode->i_sb,xid);
571 cifs_get_inode_info(&inode, "", NULL, inode->i_sb,xid);
572 /* can not call macro FreeXid here since in a void func */
576 int cifs_unlink(struct inode *inode, struct dentry *direntry)
580 struct cifs_sb_info *cifs_sb;
581 struct cifsTconInfo *pTcon;
582 char *full_path = NULL;
583 struct cifsInodeInfo *cifsInode;
584 FILE_BASIC_INFO *pinfo_buf;
586 cFYI(1, ("cifs_unlink, inode = 0x%p", inode));
591 cifs_sb = CIFS_SB(inode->i_sb);
593 cifs_sb = CIFS_SB(direntry->d_sb);
594 pTcon = cifs_sb->tcon;
596 /* Unlink can be called from rename so we can not grab the sem here
597 since we deadlock otherwise */
598 /* mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);*/
599 full_path = build_path_from_dentry(direntry);
600 /* mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);*/
601 if (full_path == NULL) {
605 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
606 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
609 if (direntry->d_inode)
610 drop_nlink(direntry->d_inode);
611 } else if (rc == -ENOENT) {
613 } else if (rc == -ETXTBSY) {
617 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
618 CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
619 &netfid, &oplock, NULL, cifs_sb->local_nls,
620 cifs_sb->mnt_cifs_flags &
621 CIFS_MOUNT_MAP_SPECIAL_CHR);
623 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
625 cifs_sb->mnt_cifs_flags &
626 CIFS_MOUNT_MAP_SPECIAL_CHR);
627 CIFSSMBClose(xid, pTcon, netfid);
628 if (direntry->d_inode)
629 drop_nlink(direntry->d_inode);
631 } else if (rc == -EACCES) {
632 /* try only if r/o attribute set in local lookup data? */
633 pinfo_buf = kzalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
635 /* ATTRS set to normal clears r/o bit */
636 pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
637 if (!(pTcon->ses->flags & CIFS_SES_NT4))
638 rc = CIFSSMBSetTimes(xid, pTcon, full_path,
641 cifs_sb->mnt_cifs_flags &
642 CIFS_MOUNT_MAP_SPECIAL_CHR);
646 if (rc == -EOPNOTSUPP) {
649 /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
653 For some strange reason it seems that NT4 eats the
654 old setattr call without actually setting the
655 attributes so on to the third attempted workaround
658 /* BB could scan to see if we already have it open
659 and pass in pid of opener to function */
660 rc = CIFSSMBOpen(xid, pTcon, full_path,
661 FILE_OPEN, SYNCHRONIZE |
662 FILE_WRITE_ATTRIBUTES, 0,
663 &netfid, &oplock, NULL,
665 cifs_sb->mnt_cifs_flags &
666 CIFS_MOUNT_MAP_SPECIAL_CHR);
668 rc = CIFSSMBSetFileTimes(xid, pTcon,
671 CIFSSMBClose(xid, pTcon, netfid);
677 rc = CIFSSMBDelFile(xid, pTcon, full_path,
679 cifs_sb->mnt_cifs_flags &
680 CIFS_MOUNT_MAP_SPECIAL_CHR);
682 if (direntry->d_inode)
683 drop_nlink(direntry->d_inode);
684 } else if (rc == -ETXTBSY) {
688 rc = CIFSSMBOpen(xid, pTcon, full_path,
691 CREATE_DELETE_ON_CLOSE,
692 &netfid, &oplock, NULL,
694 cifs_sb->mnt_cifs_flags &
695 CIFS_MOUNT_MAP_SPECIAL_CHR);
697 CIFSSMBRenameOpenFile(xid, pTcon,
700 cifs_sb->mnt_cifs_flags &
701 CIFS_MOUNT_MAP_SPECIAL_CHR);
702 CIFSSMBClose(xid, pTcon, netfid);
703 if (direntry->d_inode)
704 drop_nlink(direntry->d_inode);
706 /* BB if rc = -ETXTBUSY goto the rename logic BB */
710 if (direntry->d_inode) {
711 cifsInode = CIFS_I(direntry->d_inode);
712 cifsInode->time = 0; /* will force revalidate to get info
714 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
717 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
718 cifsInode = CIFS_I(inode);
719 cifsInode->time = 0; /* force revalidate of dir as well */
727 int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
731 struct cifs_sb_info *cifs_sb;
732 struct cifsTconInfo *pTcon;
733 char *full_path = NULL;
734 struct inode *newinode = NULL;
736 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
740 cifs_sb = CIFS_SB(inode->i_sb);
741 pTcon = cifs_sb->tcon;
743 full_path = build_path_from_dentry(direntry);
744 if (full_path == NULL) {
748 /* BB add setting the equivalent of mode via CreateX w/ACLs */
749 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
750 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
752 cFYI(1, ("cifs_mkdir returned 0x%x", rc));
756 if (pTcon->ses->capabilities & CAP_UNIX)
757 rc = cifs_get_inode_info_unix(&newinode, full_path,
760 rc = cifs_get_inode_info(&newinode, full_path, NULL,
764 direntry->d_op = &cifs_ci_dentry_ops;
766 direntry->d_op = &cifs_dentry_ops;
767 d_instantiate(direntry, newinode);
768 if (direntry->d_inode)
769 direntry->d_inode->i_nlink = 2;
770 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
771 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
772 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
774 (__u64)current->fsuid,
775 (__u64)current->fsgid,
778 cifs_sb->mnt_cifs_flags &
779 CIFS_MOUNT_MAP_SPECIAL_CHR);
781 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
783 (__u64)-1, 0 /* dev_t */,
785 cifs_sb->mnt_cifs_flags &
786 CIFS_MOUNT_MAP_SPECIAL_CHR);
789 /* BB to be implemented via Windows secrty descriptors
790 eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
791 -1, -1, local_nls); */
792 if(direntry->d_inode) {
793 direntry->d_inode->i_mode = mode;
794 direntry->d_inode->i_mode |= S_IFDIR;
795 if(cifs_sb->mnt_cifs_flags &
796 CIFS_MOUNT_SET_UID) {
797 direntry->d_inode->i_uid =
799 direntry->d_inode->i_gid =
810 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
814 struct cifs_sb_info *cifs_sb;
815 struct cifsTconInfo *pTcon;
816 char *full_path = NULL;
817 struct cifsInodeInfo *cifsInode;
819 cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
823 cifs_sb = CIFS_SB(inode->i_sb);
824 pTcon = cifs_sb->tcon;
826 full_path = build_path_from_dentry(direntry);
827 if (full_path == NULL) {
832 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
833 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
837 i_size_write(direntry->d_inode,0);
838 clear_nlink(direntry->d_inode);
841 cifsInode = CIFS_I(direntry->d_inode);
842 cifsInode->time = 0; /* force revalidate to go get info when
844 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
845 current_fs_time(inode->i_sb);
852 int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
853 struct inode *target_inode, struct dentry *target_direntry)
857 struct cifs_sb_info *cifs_sb_source;
858 struct cifs_sb_info *cifs_sb_target;
859 struct cifsTconInfo *pTcon;
865 cifs_sb_target = CIFS_SB(target_inode->i_sb);
866 cifs_sb_source = CIFS_SB(source_inode->i_sb);
867 pTcon = cifs_sb_source->tcon;
869 if (pTcon != cifs_sb_target->tcon) {
871 return -EXDEV; /* BB actually could be allowed if same server,
873 Might eventually add support for this */
876 /* we already have the rename sem so we do not need to grab it again
877 here to protect the path integrity */
878 fromName = build_path_from_dentry(source_direntry);
879 toName = build_path_from_dentry(target_direntry);
880 if ((fromName == NULL) || (toName == NULL)) {
882 goto cifs_rename_exit;
885 rc = CIFSSMBRename(xid, pTcon, fromName, toName,
886 cifs_sb_source->local_nls,
887 cifs_sb_source->mnt_cifs_flags &
888 CIFS_MOUNT_MAP_SPECIAL_CHR);
890 /* check if they are the same file because rename of hardlinked
892 FILE_UNIX_BASIC_INFO *info_buf_source;
893 FILE_UNIX_BASIC_INFO *info_buf_target;
896 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
897 if (info_buf_source != NULL) {
898 info_buf_target = info_buf_source + 1;
899 if (pTcon->ses->capabilities & CAP_UNIX)
900 rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
902 cifs_sb_source->local_nls,
903 cifs_sb_source->mnt_cifs_flags &
904 CIFS_MOUNT_MAP_SPECIAL_CHR);
905 /* else rc is still EEXIST so will fall through to
906 unlink the target and retry rename */
908 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
910 cifs_sb_target->local_nls,
911 /* remap based on source sb */
912 cifs_sb_source->mnt_cifs_flags &
913 CIFS_MOUNT_MAP_SPECIAL_CHR);
916 (info_buf_source->UniqueId ==
917 info_buf_target->UniqueId)) {
918 /* do not rename since the files are hardlinked which
921 /* we either can not tell the files are hardlinked
922 (as with Windows servers) or files are not
923 hardlinked so delete the target manually before
924 renaming to follow POSIX rather than Windows
926 cifs_unlink(target_inode, target_direntry);
927 rc = CIFSSMBRename(xid, pTcon, fromName,
929 cifs_sb_source->local_nls,
930 cifs_sb_source->mnt_cifs_flags
931 & CIFS_MOUNT_MAP_SPECIAL_CHR);
933 kfree(info_buf_source);
934 } /* if we can not get memory just leave rc as EEXIST */
938 cFYI(1, ("rename rc %d", rc));
941 if ((rc == -EIO) || (rc == -EEXIST)) {
945 /* BB FIXME Is Generic Read correct for rename? */
946 /* if renaming directory - we should not say CREATE_NOT_DIR,
947 need to test renaming open directory, also GENERIC_READ
948 might not right be right access to request */
949 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
950 CREATE_NOT_DIR, &netfid, &oplock, NULL,
951 cifs_sb_source->local_nls,
952 cifs_sb_source->mnt_cifs_flags &
953 CIFS_MOUNT_MAP_SPECIAL_CHR);
955 rc = CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
956 cifs_sb_source->local_nls,
957 cifs_sb_source->mnt_cifs_flags &
958 CIFS_MOUNT_MAP_SPECIAL_CHR);
959 CIFSSMBClose(xid, pTcon, netfid);
970 int cifs_revalidate(struct dentry *direntry)
975 struct cifs_sb_info *cifs_sb;
976 struct cifsInodeInfo *cifsInode;
978 struct timespec local_mtime;
979 int invalidate_inode = FALSE;
981 if (direntry->d_inode == NULL)
984 cifsInode = CIFS_I(direntry->d_inode);
986 if (cifsInode == NULL)
989 /* no sense revalidating inode info on file that no one can write */
990 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
995 cifs_sb = CIFS_SB(direntry->d_sb);
997 /* can not safely grab the rename sem here if rename calls revalidate
998 since that would deadlock */
999 full_path = build_path_from_dentry(direntry);
1000 if (full_path == NULL) {
1004 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
1005 "jiffies %ld", full_path, direntry->d_inode,
1006 direntry->d_inode->i_count.counter, direntry,
1007 direntry->d_time, jiffies));
1009 if (cifsInode->time == 0) {
1010 /* was set to zero previously to force revalidate */
1011 } else if (time_before(jiffies, cifsInode->time + HZ) &&
1012 lookupCacheEnabled) {
1013 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
1014 (direntry->d_inode->i_nlink == 1)) {
1019 cFYI(1, ("Have to revalidate file due to hardlinks"));
1023 /* save mtime and size */
1024 local_mtime = direntry->d_inode->i_mtime;
1025 local_size = direntry->d_inode->i_size;
1027 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
1028 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
1029 direntry->d_sb,xid);
1031 cFYI(1, ("error on getting revalidate info %d", rc));
1032 /* if (rc != -ENOENT)
1033 rc = 0; */ /* BB should we cache info on
1037 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
1038 direntry->d_sb,xid);
1040 cFYI(1, ("error on getting revalidate info %d", rc));
1041 /* if (rc != -ENOENT)
1042 rc = 0; */ /* BB should we cache info on
1046 /* should we remap certain errors, access denied?, to zero */
1048 /* if not oplocked, we invalidate inode pages if mtime or file size
1049 had changed on server */
1051 if (timespec_equal(&local_mtime,&direntry->d_inode->i_mtime) &&
1052 (local_size == direntry->d_inode->i_size)) {
1053 cFYI(1, ("cifs_revalidate - inode unchanged"));
1055 /* file may have changed on server */
1056 if (cifsInode->clientCanCacheRead) {
1057 /* no need to invalidate inode pages since we were the
1058 only ones who could have modified the file and the
1059 server copy is staler than ours */
1061 invalidate_inode = TRUE;
1065 /* can not grab this sem since kernel filesys locking documentation
1066 indicates i_mutex may be taken by the kernel on lookup and rename
1067 which could deadlock if we grab the i_mutex here as well */
1068 /* mutex_lock(&direntry->d_inode->i_mutex);*/
1069 /* need to write out dirty pages here */
1070 if (direntry->d_inode->i_mapping) {
1071 /* do we need to lock inode until after invalidate completes
1073 filemap_fdatawrite(direntry->d_inode->i_mapping);
1075 if (invalidate_inode) {
1076 /* shrink_dcache not necessary now that cifs dentry ops
1077 are exported for negative dentries */
1078 /* if(S_ISDIR(direntry->d_inode->i_mode))
1079 shrink_dcache_parent(direntry); */
1080 if (S_ISREG(direntry->d_inode->i_mode)) {
1081 if (direntry->d_inode->i_mapping)
1082 filemap_fdatawait(direntry->d_inode->i_mapping);
1083 /* may eventually have to do this for open files too */
1084 if (list_empty(&(cifsInode->openFileList))) {
1085 /* changed on server - flush read ahead pages */
1086 cFYI(1, ("Invalidating read ahead data on "
1088 invalidate_remote_inode(direntry->d_inode);
1092 /* mutex_unlock(&direntry->d_inode->i_mutex); */
1099 int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1102 int err = cifs_revalidate(dentry);
1104 generic_fillattr(dentry->d_inode, stat);
1105 stat->blksize = CIFS_MAX_MSGSIZE;
1110 static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1112 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1113 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1118 page = grab_cache_page(mapping, index);
1122 kaddr = kmap_atomic(page, KM_USER0);
1123 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1124 flush_dcache_page(page);
1125 kunmap_atomic(kaddr, KM_USER0);
1127 page_cache_release(page);
1131 int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
1134 struct cifs_sb_info *cifs_sb;
1135 struct cifsTconInfo *pTcon;
1136 char *full_path = NULL;
1138 struct cifsFileInfo *open_file = NULL;
1139 FILE_BASIC_INFO time_buf;
1140 int set_time = FALSE;
1141 __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
1142 __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
1143 __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
1144 struct cifsInodeInfo *cifsInode;
1148 cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
1149 direntry->d_name.name, attrs->ia_valid));
1151 cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
1152 pTcon = cifs_sb->tcon;
1154 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
1155 /* check if we have permission to change attrs */
1156 rc = inode_change_ok(direntry->d_inode, attrs);
1164 full_path = build_path_from_dentry(direntry);
1165 if (full_path == NULL) {
1169 cifsInode = CIFS_I(direntry->d_inode);
1171 /* BB check if we need to refresh inode from server now ? BB */
1173 /* need to flush data before changing file size on server */
1174 filemap_write_and_wait(direntry->d_inode->i_mapping);
1176 if (attrs->ia_valid & ATTR_SIZE) {
1177 /* To avoid spurious oplock breaks from server, in the case of
1178 inodes that we already have open, avoid doing path based
1179 setting of file size if we can do it by handle.
1180 This keeps our caching token (oplock) and avoids timeouts
1181 when the local oplock break takes longer to flush
1182 writebehind data than the SMB timeout for the SetPathInfo
1183 request would allow */
1185 open_file = find_writable_file(cifsInode);
1187 __u16 nfid = open_file->netfid;
1188 __u32 npid = open_file->pid;
1189 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
1191 atomic_dec(&open_file->wrtPending);
1192 cFYI(1,("SetFSize for attrs rc = %d", rc));
1193 if((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1195 rc = CIFSSMBWrite(xid, pTcon,
1196 nfid, 0, attrs->ia_size,
1197 &bytes_written, NULL, NULL,
1198 1 /* 45 seconds */);
1199 cFYI(1,("Wrt seteof rc %d", rc));
1205 /* Set file size by pathname rather than by handle
1206 either because no valid, writeable file handle for
1207 it was found or because there was an error setting
1209 rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1210 attrs->ia_size, FALSE,
1212 cifs_sb->mnt_cifs_flags &
1213 CIFS_MOUNT_MAP_SPECIAL_CHR);
1214 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1215 if((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1219 rc = SMBLegacyOpen(xid, pTcon, full_path,
1221 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1222 CREATE_NOT_DIR, &netfid, &oplock,
1223 NULL, cifs_sb->local_nls,
1224 cifs_sb->mnt_cifs_flags &
1225 CIFS_MOUNT_MAP_SPECIAL_CHR);
1228 rc = CIFSSMBWrite(xid, pTcon,
1231 &bytes_written, NULL,
1232 NULL, 1 /* 45 sec */);
1233 cFYI(1,("wrt seteof rc %d",rc));
1234 CIFSSMBClose(xid, pTcon, netfid);
1240 /* Server is ok setting allocation size implicitly - no need
1242 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
1243 cifs_sb->local_nls);
1247 rc = vmtruncate(direntry->d_inode, attrs->ia_size);
1248 cifs_truncate_page(direntry->d_inode->i_mapping,
1249 direntry->d_inode->i_size);
1251 goto cifs_setattr_exit;
1253 if (attrs->ia_valid & ATTR_UID) {
1254 cFYI(1, ("UID changed to %d", attrs->ia_uid));
1255 uid = attrs->ia_uid;
1257 if (attrs->ia_valid & ATTR_GID) {
1258 cFYI(1, ("GID changed to %d", attrs->ia_gid));
1259 gid = attrs->ia_gid;
1262 time_buf.Attributes = 0;
1263 if (attrs->ia_valid & ATTR_MODE) {
1264 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
1265 mode = attrs->ia_mode;
1268 if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX)
1269 && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
1270 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
1271 0 /* dev_t */, cifs_sb->local_nls,
1272 cifs_sb->mnt_cifs_flags &
1273 CIFS_MOUNT_MAP_SPECIAL_CHR);
1274 else if (attrs->ia_valid & ATTR_MODE) {
1276 if ((mode & S_IWUGO) == 0) /* not writeable */ {
1277 if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0)
1278 time_buf.Attributes =
1279 cpu_to_le32(cifsInode->cifsAttrs |
1281 } else if ((mode & S_IWUGO) == S_IWUGO) {
1282 if (cifsInode->cifsAttrs & ATTR_READONLY)
1283 time_buf.Attributes =
1284 cpu_to_le32(cifsInode->cifsAttrs &
1287 /* BB to be implemented -
1288 via Windows security descriptors or streams */
1289 /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid,
1290 cifs_sb->local_nls); */
1293 if (attrs->ia_valid & ATTR_ATIME) {
1295 time_buf.LastAccessTime =
1296 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1298 time_buf.LastAccessTime = 0;
1300 if (attrs->ia_valid & ATTR_MTIME) {
1302 time_buf.LastWriteTime =
1303 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1305 time_buf.LastWriteTime = 0;
1306 /* Do not set ctime explicitly unless other time
1307 stamps are changed explicitly (i.e. by utime()
1308 since we would then have a mix of client and
1311 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1313 /* Although Samba throws this field away
1314 it may be useful to Windows - but we do
1315 not want to set ctime unless some other
1316 timestamp is changing */
1317 cFYI(1, ("CIFS - CTIME changed"));
1318 time_buf.ChangeTime =
1319 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1321 time_buf.ChangeTime = 0;
1323 if (set_time || time_buf.Attributes) {
1324 time_buf.CreationTime = 0; /* do not change */
1325 /* In the future we should experiment - try setting timestamps
1326 via Handle (SetFileInfo) instead of by path */
1327 if (!(pTcon->ses->flags & CIFS_SES_NT4))
1328 rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
1330 cifs_sb->mnt_cifs_flags &
1331 CIFS_MOUNT_MAP_SPECIAL_CHR);
1335 if (rc == -EOPNOTSUPP) {
1339 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
1340 "times not supported by this server"));
1341 /* BB we could scan to see if we already have it open
1342 and pass in pid of opener to function */
1343 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1344 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1345 CREATE_NOT_DIR, &netfid, &oplock,
1346 NULL, cifs_sb->local_nls,
1347 cifs_sb->mnt_cifs_flags &
1348 CIFS_MOUNT_MAP_SPECIAL_CHR);
1350 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1352 CIFSSMBClose(xid, pTcon, netfid);
1354 /* BB For even older servers we could convert time_buf
1355 into old DOS style which uses two second
1358 /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
1359 &time_buf, cifs_sb->local_nls); */
1362 /* Even if error on time set, no sense failing the call if
1363 the server would set the time to a reasonable value anyway,
1364 and this check ensures that we are not being called from
1365 sys_utimes in which case we ought to fail the call back to
1366 the user when the server rejects the call */
1367 if((rc) && (attrs->ia_valid &
1368 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
1372 /* do not need local check to inode_check_ok since the server does
1375 rc = inode_setattr(direntry->d_inode, attrs);
1382 void cifs_delete_inode(struct inode *inode)
1384 cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
1385 /* may have to add back in if and when safe distributed caching of
1386 directories added e.g. via FindNotify */