Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * fs/cifs/inode.c |
| 3 | * |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 4 | * Copyright (C) International Business Machines Corp., 2002,2008 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Lesser General Public License as published |
| 9 | * by the Free Software Foundation; either version 2.1 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 15 | * the GNU Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/stat.h> |
| 23 | #include <linux/pagemap.h> |
| 24 | #include <asm/div64.h> |
| 25 | #include "cifsfs.h" |
| 26 | #include "cifspdu.h" |
| 27 | #include "cifsglob.h" |
| 28 | #include "cifsproto.h" |
| 29 | #include "cifs_debug.h" |
| 30 | #include "cifs_fs_sb.h" |
| 31 | |
Christoph Hellwig | 70eff55 | 2008-02-15 20:55:05 +0000 | [diff] [blame] | 32 | |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 33 | static void cifs_set_ops(struct inode *inode, const bool is_dfs_referral) |
Christoph Hellwig | 70eff55 | 2008-02-15 20:55:05 +0000 | [diff] [blame] | 34 | { |
| 35 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 36 | |
| 37 | switch (inode->i_mode & S_IFMT) { |
| 38 | case S_IFREG: |
| 39 | inode->i_op = &cifs_file_inode_ops; |
| 40 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) { |
| 41 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
| 42 | inode->i_fop = &cifs_file_direct_nobrl_ops; |
| 43 | else |
| 44 | inode->i_fop = &cifs_file_direct_ops; |
| 45 | } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
| 46 | inode->i_fop = &cifs_file_nobrl_ops; |
| 47 | else { /* not direct, send byte range locks */ |
| 48 | inode->i_fop = &cifs_file_ops; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /* check if server can support readpages */ |
| 53 | if (cifs_sb->tcon->ses->server->maxBuf < |
| 54 | PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE) |
| 55 | inode->i_data.a_ops = &cifs_addr_ops_smallbuf; |
| 56 | else |
| 57 | inode->i_data.a_ops = &cifs_addr_ops; |
| 58 | break; |
| 59 | case S_IFDIR: |
Steve French | bc5b6e2 | 2008-03-11 21:07:48 +0000 | [diff] [blame] | 60 | #ifdef CONFIG_CIFS_DFS_UPCALL |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 61 | if (is_dfs_referral) { |
| 62 | inode->i_op = &cifs_dfs_referral_inode_operations; |
| 63 | } else { |
Steve French | bc5b6e2 | 2008-03-11 21:07:48 +0000 | [diff] [blame] | 64 | #else /* NO DFS support, treat as a directory */ |
| 65 | { |
| 66 | #endif |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 67 | inode->i_op = &cifs_dir_inode_ops; |
| 68 | inode->i_fop = &cifs_dir_ops; |
| 69 | } |
Christoph Hellwig | 70eff55 | 2008-02-15 20:55:05 +0000 | [diff] [blame] | 70 | break; |
| 71 | case S_IFLNK: |
| 72 | inode->i_op = &cifs_symlink_inode_ops; |
| 73 | break; |
| 74 | default: |
| 75 | init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 80 | /* populate an inode with info from a cifs_fattr struct */ |
| 81 | void |
| 82 | cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr) |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 83 | { |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 84 | struct cifsInodeInfo *cifs_i = CIFS_I(inode); |
| 85 | unsigned long now = jiffies; |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 86 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 87 | inode->i_atime = fattr->cf_atime; |
| 88 | inode->i_mtime = fattr->cf_mtime; |
| 89 | inode->i_ctime = fattr->cf_ctime; |
| 90 | inode->i_mode = fattr->cf_mode; |
| 91 | inode->i_rdev = fattr->cf_rdev; |
| 92 | inode->i_nlink = fattr->cf_nlink; |
| 93 | inode->i_uid = fattr->cf_uid; |
| 94 | inode->i_gid = fattr->cf_gid; |
| 95 | |
| 96 | cifs_i->cifsAttrs = fattr->cf_cifsattrs; |
| 97 | cifs_i->uniqueid = fattr->cf_uniqueid; |
| 98 | |
| 99 | cFYI(1, ("inode 0x%p old_time=%ld new_time=%ld", inode, |
| 100 | cifs_i->time, now)); |
| 101 | cifs_i->time = now; |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 102 | |
| 103 | /* |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 104 | * Can't safely change the file size here if the client is writing to |
| 105 | * it due to potential races. |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 106 | */ |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 107 | spin_lock(&inode->i_lock); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 108 | if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) { |
| 109 | i_size_write(inode, fattr->cf_eof); |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * i_blocks is not related to (i_size / i_blksize), |
| 113 | * but instead 512 byte (2**9) size is required for |
| 114 | * calculating num blocks. |
| 115 | */ |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 116 | inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9; |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 117 | } |
| 118 | spin_unlock(&inode->i_lock); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 119 | |
| 120 | cifs_set_ops(inode, fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL); |
Christoph Hellwig | 75f1298 | 2008-02-25 20:25:21 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 123 | /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */ |
| 124 | void |
| 125 | cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info, |
| 126 | struct cifs_sb_info *cifs_sb) |
| 127 | { |
| 128 | memset(fattr, 0, sizeof(*fattr)); |
| 129 | fattr->cf_uniqueid = le64_to_cpu(info->UniqueId); |
| 130 | fattr->cf_bytes = le64_to_cpu(info->NumOfBytes); |
| 131 | fattr->cf_eof = le64_to_cpu(info->EndOfFile); |
| 132 | |
| 133 | fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime); |
| 134 | fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime); |
| 135 | fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange); |
| 136 | fattr->cf_mode = le64_to_cpu(info->Permissions); |
| 137 | |
| 138 | /* |
| 139 | * Since we set the inode type below we need to mask off |
| 140 | * to avoid strange results if bits set above. |
| 141 | */ |
| 142 | fattr->cf_mode &= ~S_IFMT; |
| 143 | switch (le32_to_cpu(info->Type)) { |
| 144 | case UNIX_FILE: |
| 145 | fattr->cf_mode |= S_IFREG; |
| 146 | fattr->cf_dtype = DT_REG; |
| 147 | break; |
| 148 | case UNIX_SYMLINK: |
| 149 | fattr->cf_mode |= S_IFLNK; |
| 150 | fattr->cf_dtype = DT_LNK; |
| 151 | break; |
| 152 | case UNIX_DIR: |
| 153 | fattr->cf_mode |= S_IFDIR; |
| 154 | fattr->cf_dtype = DT_DIR; |
| 155 | break; |
| 156 | case UNIX_CHARDEV: |
| 157 | fattr->cf_mode |= S_IFCHR; |
| 158 | fattr->cf_dtype = DT_CHR; |
| 159 | fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor), |
| 160 | le64_to_cpu(info->DevMinor) & MINORMASK); |
| 161 | break; |
| 162 | case UNIX_BLOCKDEV: |
| 163 | fattr->cf_mode |= S_IFBLK; |
| 164 | fattr->cf_dtype = DT_BLK; |
| 165 | fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor), |
| 166 | le64_to_cpu(info->DevMinor) & MINORMASK); |
| 167 | break; |
| 168 | case UNIX_FIFO: |
| 169 | fattr->cf_mode |= S_IFIFO; |
| 170 | fattr->cf_dtype = DT_FIFO; |
| 171 | break; |
| 172 | case UNIX_SOCKET: |
| 173 | fattr->cf_mode |= S_IFSOCK; |
| 174 | fattr->cf_dtype = DT_SOCK; |
| 175 | break; |
| 176 | default: |
| 177 | /* safest to call it a file if we do not know */ |
| 178 | fattr->cf_mode |= S_IFREG; |
| 179 | fattr->cf_dtype = DT_REG; |
| 180 | cFYI(1, ("unknown type %d", le32_to_cpu(info->Type))); |
| 181 | break; |
| 182 | } |
| 183 | |
| 184 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) |
| 185 | fattr->cf_uid = cifs_sb->mnt_uid; |
| 186 | else |
| 187 | fattr->cf_uid = le64_to_cpu(info->Uid); |
| 188 | |
| 189 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) |
| 190 | fattr->cf_gid = cifs_sb->mnt_gid; |
| 191 | else |
| 192 | fattr->cf_gid = le64_to_cpu(info->Gid); |
| 193 | |
| 194 | fattr->cf_nlink = le64_to_cpu(info->Nlinks); |
| 195 | } |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 196 | |
| 197 | /* |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 198 | * Fill a cifs_fattr struct with fake inode info. |
| 199 | * |
| 200 | * Needed to setup cifs_fattr data for the directory which is the |
| 201 | * junction to the new submount (ie to setup the fake directory |
| 202 | * which represents a DFS referral). |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 203 | */ |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 204 | void |
| 205 | cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb) |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 206 | { |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 207 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 208 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 209 | cFYI(1, ("creating fake fattr for DFS referral")); |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 210 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 211 | memset(fattr, 0, sizeof(*fattr)); |
| 212 | fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU; |
| 213 | fattr->cf_uid = cifs_sb->mnt_uid; |
| 214 | fattr->cf_gid = cifs_sb->mnt_gid; |
| 215 | fattr->cf_atime = CURRENT_TIME; |
| 216 | fattr->cf_ctime = CURRENT_TIME; |
| 217 | fattr->cf_mtime = CURRENT_TIME; |
| 218 | fattr->cf_nlink = 2; |
| 219 | fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL; |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 222 | /** |
| 223 | * cifs_new inode - create new inode, initialize, and hash it |
| 224 | * @sb - pointer to superblock |
| 225 | * @inum - if valid pointer and serverino is enabled, replace i_ino with val |
| 226 | * |
| 227 | * Create a new inode, initialize it for CIFS and hash it. Returns the new |
| 228 | * inode or NULL if one couldn't be allocated. |
| 229 | * |
| 230 | * If the share isn't mounted with "serverino" or inum is a NULL pointer then |
| 231 | * we'll just use the inode number assigned by new_inode(). Note that this can |
| 232 | * mean i_ino collisions since the i_ino assigned by new_inode is not |
| 233 | * guaranteed to be unique. |
| 234 | */ |
| 235 | struct inode * |
Jeff Layton | 950ec52 | 2009-02-11 08:08:26 -0500 | [diff] [blame] | 236 | cifs_new_inode(struct super_block *sb, __u64 *inum) |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 237 | { |
| 238 | struct inode *inode; |
| 239 | |
| 240 | inode = new_inode(sb); |
| 241 | if (inode == NULL) |
| 242 | return NULL; |
| 243 | |
| 244 | /* |
| 245 | * BB: Is i_ino == 0 legal? Here, we assume that it is. If it isn't we |
| 246 | * stop passing inum as ptr. Are there sanity checks we can use to |
| 247 | * ensure that the server is really filling in that field? Also, |
| 248 | * if serverino is disabled, perhaps we should be using iunique()? |
| 249 | */ |
| 250 | if (inum && (CIFS_SB(sb)->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) |
Jeff Layton | 950ec52 | 2009-02-11 08:08:26 -0500 | [diff] [blame] | 251 | inode->i_ino = (unsigned long) *inum; |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * must set this here instead of cifs_alloc_inode since VFS will |
| 255 | * clobber i_flags |
| 256 | */ |
| 257 | if (sb->s_flags & MS_NOATIME) |
| 258 | inode->i_flags |= S_NOATIME | S_NOCMTIME; |
| 259 | |
| 260 | insert_inode_hash(inode); |
| 261 | |
| 262 | return inode; |
| 263 | } |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | int cifs_get_inode_info_unix(struct inode **pinode, |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 266 | const unsigned char *full_path, |
| 267 | struct super_block *sb, int xid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 269 | int rc; |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 270 | FILE_UNIX_BASIC_INFO find_data; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 271 | struct cifs_fattr fattr; |
| 272 | struct cifsTconInfo *tcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 275 | tcon = cifs_sb->tcon; |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 276 | cFYI(1, ("Getting info on %s", full_path)); |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | /* could have done a find first instead but this returns more info */ |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 279 | rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 280 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
| 281 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 282 | |
| 283 | if (!rc) { |
| 284 | cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb); |
| 285 | } else if (rc == -EREMOTE) { |
| 286 | cifs_create_dfs_fattr(&fattr, sb); |
Jeff Layton | e911d0c | 2008-07-12 13:47:59 -0700 | [diff] [blame] | 287 | rc = 0; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 288 | } else { |
| 289 | return rc; |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 292 | if (*pinode == NULL) { |
| 293 | /* get new inode */ |
| 294 | *pinode = cifs_iget(sb, &fattr); |
| 295 | if (!*pinode) |
| 296 | rc = -ENOMEM; |
| 297 | } else { |
| 298 | /* we already have inode, update it */ |
| 299 | cifs_fattr_to_inode(*pinode, &fattr); |
| 300 | } |
Steve French | 0e4bbde | 2008-05-20 19:50:46 +0000 | [diff] [blame] | 301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | return rc; |
| 303 | } |
| 304 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 305 | static int decode_sfu_inode(struct inode *inode, __u64 size, |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 306 | const unsigned char *path, |
| 307 | struct cifs_sb_info *cifs_sb, int xid) |
| 308 | { |
| 309 | int rc; |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 310 | int oplock = 0; |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 311 | __u16 netfid; |
| 312 | struct cifsTconInfo *pTcon = cifs_sb->tcon; |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 313 | char buf[24]; |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 314 | unsigned int bytes_read; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 315 | char *pbuf; |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 316 | |
| 317 | pbuf = buf; |
| 318 | |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 319 | if (size == 0) { |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 320 | inode->i_mode |= S_IFIFO; |
| 321 | return 0; |
| 322 | } else if (size < 8) { |
| 323 | return -EINVAL; /* EOPNOTSUPP? */ |
| 324 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 325 | |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 326 | rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ, |
| 327 | CREATE_NOT_DIR, &netfid, &oplock, NULL, |
| 328 | cifs_sb->local_nls, |
| 329 | cifs_sb->mnt_cifs_flags & |
| 330 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 331 | if (rc == 0) { |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 332 | int buf_type = CIFS_NO_BUFFER; |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 333 | /* Read header */ |
| 334 | rc = CIFSSMBRead(xid, pTcon, |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 335 | netfid, |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 336 | 24 /* length */, 0 /* offset */, |
Steve French | ec637e3 | 2005-12-12 20:53:18 -0800 | [diff] [blame] | 337 | &bytes_read, &pbuf, &buf_type); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 338 | if ((rc == 0) && (bytes_read >= 8)) { |
| 339 | if (memcmp("IntxBLK", pbuf, 8) == 0) { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 340 | cFYI(1, ("Block device")); |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 341 | inode->i_mode |= S_IFBLK; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 342 | if (bytes_read == 24) { |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 343 | /* we have enough to decode dev num */ |
| 344 | __u64 mjr; /* major */ |
| 345 | __u64 mnr; /* minor */ |
| 346 | mjr = le64_to_cpu(*(__le64 *)(pbuf+8)); |
| 347 | mnr = le64_to_cpu(*(__le64 *)(pbuf+16)); |
| 348 | inode->i_rdev = MKDEV(mjr, mnr); |
| 349 | } |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 350 | } else if (memcmp("IntxCHR", pbuf, 8) == 0) { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 351 | cFYI(1, ("Char device")); |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 352 | inode->i_mode |= S_IFCHR; |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 353 | if (bytes_read == 24) { |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 354 | /* we have enough to decode dev num */ |
| 355 | __u64 mjr; /* major */ |
| 356 | __u64 mnr; /* minor */ |
| 357 | mjr = le64_to_cpu(*(__le64 *)(pbuf+8)); |
| 358 | mnr = le64_to_cpu(*(__le64 *)(pbuf+16)); |
| 359 | inode->i_rdev = MKDEV(mjr, mnr); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 360 | } |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 361 | } else if (memcmp("IntxLNK", pbuf, 7) == 0) { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 362 | cFYI(1, ("Symlink")); |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 363 | inode->i_mode |= S_IFLNK; |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 364 | } else { |
| 365 | inode->i_mode |= S_IFREG; /* file? */ |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 366 | rc = -EOPNOTSUPP; |
Steve French | 86c96b4 | 2005-11-18 20:25:31 -0800 | [diff] [blame] | 367 | } |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 368 | } else { |
| 369 | inode->i_mode |= S_IFREG; /* then it is a file */ |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 370 | rc = -EOPNOTSUPP; /* or some unknown SFU type */ |
| 371 | } |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 372 | CIFSSMBClose(xid, pTcon, netfid); |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 373 | } |
| 374 | return rc; |
Steve French | d6e2f2a4 | 2005-11-15 16:43:39 -0800 | [diff] [blame] | 375 | } |
| 376 | |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 377 | #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */ |
| 378 | |
Steve French | 953f868 | 2007-10-31 04:54:42 +0000 | [diff] [blame] | 379 | static int get_sfu_mode(struct inode *inode, |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 380 | const unsigned char *path, |
| 381 | struct cifs_sb_info *cifs_sb, int xid) |
| 382 | { |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 383 | #ifdef CONFIG_CIFS_XATTR |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 384 | ssize_t rc; |
| 385 | char ea_value[4]; |
| 386 | __u32 mode; |
| 387 | |
| 388 | rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS", |
| 389 | ea_value, 4 /* size of buf */, cifs_sb->local_nls, |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 390 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 391 | if (rc < 0) |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 392 | return (int)rc; |
| 393 | else if (rc > 3) { |
| 394 | mode = le32_to_cpu(*((__le32 *)ea_value)); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 395 | inode->i_mode &= ~SFBITS_MASK; |
| 396 | cFYI(1, ("special bits 0%o org mode 0%o", mode, inode->i_mode)); |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 397 | inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 398 | cFYI(1, ("special mode bits 0%o", mode)); |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 399 | return 0; |
| 400 | } else { |
| 401 | return 0; |
| 402 | } |
Steve French | 3020a1f | 2005-11-18 11:31:10 -0800 | [diff] [blame] | 403 | #else |
| 404 | return -EOPNOTSUPP; |
| 405 | #endif |
Steve French | 9e294f1 | 2005-11-17 16:59:21 -0800 | [diff] [blame] | 406 | } |
| 407 | |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 408 | /* |
| 409 | * Needed to setup inode data for the directory which is the |
| 410 | * junction to the new submount (ie to setup the fake directory |
| 411 | * which represents a DFS referral) |
| 412 | */ |
| 413 | static void fill_fake_finddata(FILE_ALL_INFO *pfnd_dat, |
| 414 | struct super_block *sb) |
| 415 | { |
Dave Jones | 0a891ad | 2008-05-22 14:20:21 +0000 | [diff] [blame] | 416 | memset(pfnd_dat, 0, sizeof(FILE_ALL_INFO)); |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 417 | |
| 418 | /* __le64 pfnd_dat->AllocationSize = cpu_to_le64(0); |
| 419 | __le64 pfnd_dat->EndOfFile = cpu_to_le64(0); |
| 420 | __u8 pfnd_dat->DeletePending = 0; |
| 421 | __u8 pfnd_data->Directory = 0; |
| 422 | __le32 pfnd_dat->EASize = 0; |
| 423 | __u64 pfnd_dat->IndexNumber = 0; |
| 424 | __u64 pfnd_dat->IndexNumber1 = 0; */ |
| 425 | pfnd_dat->CreationTime = |
| 426 | cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); |
| 427 | pfnd_dat->LastAccessTime = |
| 428 | cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); |
| 429 | pfnd_dat->LastWriteTime = |
| 430 | cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); |
| 431 | pfnd_dat->ChangeTime = |
| 432 | cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME)); |
| 433 | pfnd_dat->Attributes = cpu_to_le32(ATTR_DIRECTORY); |
| 434 | pfnd_dat->NumberOfLinks = cpu_to_le32(2); |
| 435 | } |
| 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | int cifs_get_inode_info(struct inode **pinode, |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 438 | const unsigned char *full_path, FILE_ALL_INFO *pfindData, |
Steve French | 8b1327f | 2008-03-14 22:37:16 +0000 | [diff] [blame] | 439 | struct super_block *sb, int xid, const __u16 *pfid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
| 441 | int rc = 0; |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 442 | __u32 attr; |
| 443 | struct cifsInodeInfo *cifsInfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | struct cifsTconInfo *pTcon; |
| 445 | struct inode *inode; |
| 446 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | char *buf = NULL; |
Steve French | 5ade9de | 2008-05-02 20:56:23 +0000 | [diff] [blame] | 448 | bool adjustTZ = false; |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 449 | bool is_dfs_referral = false; |
Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 450 | umode_t default_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | pTcon = cifs_sb->tcon; |
Steve French | 646dd53 | 2008-05-15 01:50:56 +0000 | [diff] [blame] | 453 | cFYI(1, ("Getting info on %s", full_path)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Steve French | d0d2f2d | 2005-06-02 15:12:36 -0700 | [diff] [blame] | 455 | if ((pfindData == NULL) && (*pinode != NULL)) { |
| 456 | if (CIFS_I(*pinode)->clientCanCacheRead) { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 457 | cFYI(1, ("No need to revalidate cached inode sizes")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | return rc; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /* if file info not passed in then get it from server */ |
Steve French | d0d2f2d | 2005-06-02 15:12:36 -0700 | [diff] [blame] | 463 | if (pfindData == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
Steve French | d0d2f2d | 2005-06-02 15:12:36 -0700 | [diff] [blame] | 465 | if (buf == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | return -ENOMEM; |
| 467 | pfindData = (FILE_ALL_INFO *)buf; |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | /* could do find first instead but this returns more info */ |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 470 | rc = CIFSSMBQPathInfo(xid, pTcon, full_path, pfindData, |
Steve French | acf1a1b | 2006-10-12 03:28:28 +0000 | [diff] [blame] | 471 | 0 /* not legacy */, |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 472 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 473 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 474 | /* BB optimize code so we do not make the above call |
| 475 | when server claims no NT SMB support and the above call |
| 476 | failed at least once - set flag in tcon or mount */ |
Steve French | 4523cc3 | 2007-04-30 20:13:06 +0000 | [diff] [blame] | 477 | if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) { |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 478 | rc = SMBQueryInformation(xid, pTcon, full_path, |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 479 | pfindData, cifs_sb->local_nls, |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 480 | cifs_sb->mnt_cifs_flags & |
| 481 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 482 | adjustTZ = true; |
Steve French | 6b8edfe | 2005-08-23 20:26:03 -0700 | [diff] [blame] | 483 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | } |
| 485 | /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */ |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 486 | if (rc == -EREMOTE) { |
| 487 | is_dfs_referral = true; |
| 488 | fill_fake_finddata(pfindData, sb); |
| 489 | rc = 0; |
| 490 | } else if (rc) |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 491 | goto cgii_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 493 | attr = le32_to_cpu(pfindData->Attributes); |
| 494 | |
| 495 | /* get new inode */ |
| 496 | if (*pinode == NULL) { |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 497 | __u64 inode_num; |
Jeff Layton | 950ec52 | 2009-02-11 08:08:26 -0500 | [diff] [blame] | 498 | __u64 *pinum = &inode_num; |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 499 | |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 500 | /* Is an i_ino of zero legal? Can we use that to check |
| 501 | if the server supports returning inode numbers? Are |
| 502 | there other sanity checks we can use to ensure that |
| 503 | the server is really filling in that field? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 505 | /* We can not use the IndexNumber field by default from |
| 506 | Windows or Samba (in ALL_INFO buf) but we can request |
| 507 | it explicitly. It may not be unique presumably if |
| 508 | the server has multiple devices mounted under one share */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 510 | /* There may be higher info levels that work but are |
| 511 | there Windows server or network appliances for which |
| 512 | IndexNumber field is not guaranteed unique? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 514 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { |
| 515 | int rc1 = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 517 | rc1 = CIFSGetSrvInodeNumber(xid, pTcon, |
Jeff Layton | 950ec52 | 2009-02-11 08:08:26 -0500 | [diff] [blame] | 518 | full_path, pinum, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 519 | cifs_sb->local_nls, |
| 520 | cifs_sb->mnt_cifs_flags & |
| 521 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 522 | if (rc1) { |
| 523 | cFYI(1, ("GetSrvInodeNum rc %d", rc1)); |
Jeff Layton | 950ec52 | 2009-02-11 08:08:26 -0500 | [diff] [blame] | 524 | pinum = NULL; |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 525 | /* BB EOPNOSUPP disable SERVER_INUM? */ |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 526 | } |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 527 | } else { |
Jeff Layton | 950ec52 | 2009-02-11 08:08:26 -0500 | [diff] [blame] | 528 | pinum = NULL; |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 529 | } |
| 530 | |
Jeff Layton | 950ec52 | 2009-02-11 08:08:26 -0500 | [diff] [blame] | 531 | *pinode = cifs_new_inode(sb, pinum); |
Jeff Layton | 132ac7b | 2009-02-10 07:33:57 -0500 | [diff] [blame] | 532 | if (*pinode == NULL) { |
| 533 | rc = -ENOMEM; |
| 534 | goto cgii_exit; |
| 535 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 537 | inode = *pinode; |
| 538 | cifsInfo = CIFS_I(inode); |
| 539 | cifsInfo->cifsAttrs = attr; |
Jeff Layton | 9a8165f | 2008-10-17 21:03:20 -0400 | [diff] [blame] | 540 | cifsInfo->delete_pending = pfindData->DeletePending ? true : false; |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 541 | cFYI(1, ("Old time %ld", cifsInfo->time)); |
| 542 | cifsInfo->time = jiffies; |
| 543 | cFYI(1, ("New time %ld", cifsInfo->time)); |
| 544 | |
| 545 | /* blksize needs to be multiple of two. So safer to default to |
| 546 | blksize and blkbits set in superblock so 2**blkbits and blksize |
| 547 | will match rather than setting to: |
| 548 | (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/ |
| 549 | |
| 550 | /* Linux can not store file creation time so ignore it */ |
| 551 | if (pfindData->LastAccessTime) |
Jeff Layton | 07119a4 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 552 | inode->i_atime = cifs_NTtimeToUnix(pfindData->LastAccessTime); |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 553 | else /* do not need to use current_fs_time - time not stored */ |
| 554 | inode->i_atime = CURRENT_TIME; |
Jeff Layton | 07119a4 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 555 | inode->i_mtime = cifs_NTtimeToUnix(pfindData->LastWriteTime); |
| 556 | inode->i_ctime = cifs_NTtimeToUnix(pfindData->ChangeTime); |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 557 | cFYI(DBG2, ("Attributes came in as 0x%x", attr)); |
| 558 | if (adjustTZ && (pTcon->ses) && (pTcon->ses->server)) { |
| 559 | inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj; |
| 560 | inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj; |
| 561 | } |
| 562 | |
Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 563 | /* get default inode mode */ |
| 564 | if (attr & ATTR_DIRECTORY) |
| 565 | default_mode = cifs_sb->mnt_dir_mode; |
| 566 | else |
| 567 | default_mode = cifs_sb->mnt_file_mode; |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 568 | |
Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 569 | /* set permission bits */ |
| 570 | if (atomic_read(&cifsInfo->inUse) == 0 || |
| 571 | (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0) |
| 572 | inode->i_mode = default_mode; |
| 573 | else { |
| 574 | /* just reenable write bits if !ATTR_READONLY */ |
| 575 | if ((inode->i_mode & S_IWUGO) == 0 && |
| 576 | (attr & ATTR_READONLY) == 0) |
| 577 | inode->i_mode |= (S_IWUGO & default_mode); |
Ilpo Järvinen | aab3a8c | 2008-08-19 14:23:37 +0000 | [diff] [blame] | 578 | |
| 579 | inode->i_mode &= ~S_IFMT; |
Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 580 | } |
| 581 | /* clear write bits if ATTR_READONLY is set */ |
| 582 | if (attr & ATTR_READONLY) |
| 583 | inode->i_mode &= ~S_IWUGO; |
| 584 | |
| 585 | /* set inode type */ |
| 586 | if ((attr & ATTR_SYSTEM) && |
| 587 | (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) { |
| 588 | /* no need to fix endianness on 0 */ |
| 589 | if (pfindData->EndOfFile == 0) |
| 590 | inode->i_mode |= S_IFIFO; |
| 591 | else if (decode_sfu_inode(inode, |
| 592 | le64_to_cpu(pfindData->EndOfFile), |
| 593 | full_path, cifs_sb, xid)) |
| 594 | cFYI(1, ("unknown SFU file type\n")); |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 595 | } else { |
Jeff Layton | 4468eb3 | 2008-05-22 09:31:40 -0400 | [diff] [blame] | 596 | if (attr & ATTR_DIRECTORY) |
| 597 | inode->i_mode |= S_IFDIR; |
| 598 | else |
| 599 | inode->i_mode |= S_IFREG; |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 602 | cifsInfo->server_eof = le64_to_cpu(pfindData->EndOfFile); |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 603 | spin_lock(&inode->i_lock); |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 604 | if (is_size_safe_to_change(cifsInfo, cifsInfo->server_eof)) { |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 605 | /* can not safely shrink the file size here if the |
| 606 | client is writing to it due to potential races */ |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 607 | i_size_write(inode, cifsInfo->server_eof); |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 608 | |
| 609 | /* 512 bytes (2**9) is the fake blocksize that must be |
| 610 | used for this calculation */ |
| 611 | inode->i_blocks = (512 - 1 + le64_to_cpu( |
| 612 | pfindData->AllocationSize)) >> 9; |
| 613 | } |
| 614 | spin_unlock(&inode->i_lock); |
| 615 | |
| 616 | inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks); |
| 617 | |
| 618 | /* BB fill in uid and gid here? with help from winbind? |
| 619 | or retrieve from NTFS stream extended attribute */ |
| 620 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
| 621 | /* fill in 0777 bits from ACL */ |
| 622 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) { |
| 623 | cFYI(1, ("Getting mode bits from ACL")); |
Christoph Hellwig | 1bf4072 | 2009-05-27 09:37:33 -0400 | [diff] [blame] | 624 | acl_to_uid_mode(cifs_sb, inode, full_path, pfid); |
Steve French | b9a3260f | 2008-05-20 21:52:32 +0000 | [diff] [blame] | 625 | } |
| 626 | #endif |
| 627 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) { |
| 628 | /* fill in remaining high mode bits e.g. SUID, VTX */ |
| 629 | get_sfu_mode(inode, full_path, cifs_sb, xid); |
| 630 | } else if (atomic_read(&cifsInfo->inUse) == 0) { |
| 631 | inode->i_uid = cifs_sb->mnt_uid; |
| 632 | inode->i_gid = cifs_sb->mnt_gid; |
| 633 | /* set so we do not keep refreshing these fields with |
| 634 | bad data after user has changed them in memory */ |
| 635 | atomic_set(&cifsInfo->inUse, 1); |
| 636 | } |
| 637 | |
| 638 | cifs_set_ops(inode, is_dfs_referral); |
| 639 | |
| 640 | |
| 641 | |
| 642 | |
Igor Mammedov | 7962670 | 2008-03-09 03:44:18 +0000 | [diff] [blame] | 643 | cgii_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | kfree(buf); |
| 645 | return rc; |
| 646 | } |
| 647 | |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 648 | static const struct inode_operations cifs_ipc_inode_ops = { |
| 649 | .lookup = cifs_lookup, |
| 650 | }; |
| 651 | |
Igor Mammedov | e4cce94 | 2009-02-10 14:10:26 +0300 | [diff] [blame] | 652 | char *cifs_build_path_to_root(struct cifs_sb_info *cifs_sb) |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 653 | { |
| 654 | int pplen = cifs_sb->prepathlen; |
| 655 | int dfsplen; |
| 656 | char *full_path = NULL; |
| 657 | |
| 658 | /* if no prefix path, simply set path to the root of share to "" */ |
| 659 | if (pplen == 0) { |
| 660 | full_path = kmalloc(1, GFP_KERNEL); |
| 661 | if (full_path) |
| 662 | full_path[0] = 0; |
| 663 | return full_path; |
| 664 | } |
| 665 | |
| 666 | if (cifs_sb->tcon && (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS)) |
| 667 | dfsplen = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE + 1); |
| 668 | else |
| 669 | dfsplen = 0; |
| 670 | |
| 671 | full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL); |
| 672 | if (full_path == NULL) |
| 673 | return full_path; |
| 674 | |
| 675 | if (dfsplen) { |
| 676 | strncpy(full_path, cifs_sb->tcon->treeName, dfsplen); |
| 677 | /* switch slash direction in prepath depending on whether |
| 678 | * windows or posix style path names |
| 679 | */ |
| 680 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) { |
| 681 | int i; |
| 682 | for (i = 0; i < dfsplen; i++) { |
| 683 | if (full_path[i] == '\\') |
| 684 | full_path[i] = '/'; |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | strncpy(full_path + dfsplen, cifs_sb->prepath, pplen); |
| 689 | full_path[dfsplen + pplen] = 0; /* add trailing null */ |
| 690 | return full_path; |
| 691 | } |
| 692 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 693 | static int |
| 694 | cifs_find_inode(struct inode *inode, void *opaque) |
| 695 | { |
| 696 | struct cifs_fattr *fattr = (struct cifs_fattr *) opaque; |
| 697 | |
| 698 | if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid) |
| 699 | return 0; |
| 700 | |
| 701 | return 1; |
| 702 | } |
| 703 | |
| 704 | static int |
| 705 | cifs_init_inode(struct inode *inode, void *opaque) |
| 706 | { |
| 707 | struct cifs_fattr *fattr = (struct cifs_fattr *) opaque; |
| 708 | |
| 709 | CIFS_I(inode)->uniqueid = fattr->cf_uniqueid; |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | /* Given fattrs, get a corresponding inode */ |
| 714 | struct inode * |
| 715 | cifs_iget(struct super_block *sb, struct cifs_fattr *fattr) |
| 716 | { |
| 717 | unsigned long hash; |
| 718 | struct inode *inode; |
| 719 | |
| 720 | cFYI(1, ("looking for uniqueid=%llu", fattr->cf_uniqueid)); |
| 721 | |
| 722 | /* hash down to 32-bits on 32-bit arch */ |
| 723 | hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid); |
| 724 | |
| 725 | inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr); |
| 726 | |
| 727 | /* we have fattrs in hand, update the inode */ |
| 728 | if (inode) { |
| 729 | cifs_fattr_to_inode(inode, fattr); |
| 730 | if (sb->s_flags & MS_NOATIME) |
| 731 | inode->i_flags |= S_NOATIME | S_NOCMTIME; |
| 732 | if (inode->i_state & I_NEW) { |
| 733 | inode->i_ino = hash; |
| 734 | unlock_new_inode(inode); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | return inode; |
| 739 | } |
| 740 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | /* gets root inode */ |
Jeff Layton | bd433d4 | 2009-05-27 09:37:34 -0400 | [diff] [blame] | 742 | struct inode *cifs_root_iget(struct super_block *sb, unsigned long ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | { |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 744 | int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | struct cifs_sb_info *cifs_sb; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 746 | struct inode *inode = NULL; |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 747 | long rc; |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 748 | char *full_path; |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 749 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 750 | cifs_sb = CIFS_SB(sb); |
Igor Mammedov | e4cce94 | 2009-02-10 14:10:26 +0300 | [diff] [blame] | 751 | full_path = cifs_build_path_to_root(cifs_sb); |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 752 | if (full_path == NULL) |
| 753 | return ERR_PTR(-ENOMEM); |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 754 | |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 755 | xid = GetXid(); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 756 | if (cifs_sb->tcon->unix_ext) { |
| 757 | rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid); |
| 758 | if (!inode) |
| 759 | return ERR_PTR(-ENOMEM); |
| 760 | } else { |
| 761 | inode = iget_locked(sb, ino); |
| 762 | if (!inode) |
| 763 | return ERR_PTR(-ENOMEM); |
| 764 | if (!(inode->i_state & I_NEW)) |
| 765 | return inode; |
| 766 | |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 767 | rc = cifs_get_inode_info(&inode, full_path, NULL, inode->i_sb, |
| 768 | xid, NULL); |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 769 | unlock_new_inode(inode); |
| 770 | } |
| 771 | |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 772 | if (rc && cifs_sb->tcon->ipc) { |
| 773 | cFYI(1, ("ipc connection - fake read inode")); |
| 774 | inode->i_mode |= S_IFDIR; |
| 775 | inode->i_nlink = 2; |
| 776 | inode->i_op = &cifs_ipc_inode_ops; |
| 777 | inode->i_fop = &simple_dir_operations; |
| 778 | inode->i_uid = cifs_sb->mnt_uid; |
| 779 | inode->i_gid = cifs_sb->mnt_gid; |
Steve French | ad66133 | 2008-08-12 14:14:40 +0000 | [diff] [blame] | 780 | } else if (rc) { |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 781 | kfree(full_path); |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 782 | _FreeXid(xid); |
| 783 | iget_failed(inode); |
| 784 | return ERR_PTR(rc); |
Steve French | 7f8ed42 | 2007-09-28 22:28:55 +0000 | [diff] [blame] | 785 | } |
| 786 | |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 787 | |
Steve French | 8be0ed4 | 2008-12-05 19:14:12 +0000 | [diff] [blame] | 788 | kfree(full_path); |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 789 | /* can not call macro FreeXid here since in a void func |
| 790 | * TODO: This is no longer true |
| 791 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | _FreeXid(xid); |
David Howells | ce634ab | 2008-02-07 00:15:33 -0800 | [diff] [blame] | 793 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | } |
| 795 | |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 796 | static int |
| 797 | cifs_set_file_info(struct inode *inode, struct iattr *attrs, int xid, |
| 798 | char *full_path, __u32 dosattr) |
| 799 | { |
| 800 | int rc; |
| 801 | int oplock = 0; |
| 802 | __u16 netfid; |
| 803 | __u32 netpid; |
| 804 | bool set_time = false; |
| 805 | struct cifsFileInfo *open_file; |
| 806 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 807 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 808 | struct cifsTconInfo *pTcon = cifs_sb->tcon; |
| 809 | FILE_BASIC_INFO info_buf; |
| 810 | |
Steve French | 1adcb71 | 2009-02-25 14:19:56 +0000 | [diff] [blame] | 811 | if (attrs == NULL) |
| 812 | return -EINVAL; |
| 813 | |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 814 | if (attrs->ia_valid & ATTR_ATIME) { |
| 815 | set_time = true; |
| 816 | info_buf.LastAccessTime = |
| 817 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime)); |
| 818 | } else |
| 819 | info_buf.LastAccessTime = 0; |
| 820 | |
| 821 | if (attrs->ia_valid & ATTR_MTIME) { |
| 822 | set_time = true; |
| 823 | info_buf.LastWriteTime = |
| 824 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime)); |
| 825 | } else |
| 826 | info_buf.LastWriteTime = 0; |
| 827 | |
| 828 | /* |
| 829 | * Samba throws this field away, but windows may actually use it. |
| 830 | * Do not set ctime unless other time stamps are changed explicitly |
| 831 | * (i.e. by utimes()) since we would then have a mix of client and |
| 832 | * server times. |
| 833 | */ |
| 834 | if (set_time && (attrs->ia_valid & ATTR_CTIME)) { |
| 835 | cFYI(1, ("CIFS - CTIME changed")); |
| 836 | info_buf.ChangeTime = |
| 837 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime)); |
| 838 | } else |
| 839 | info_buf.ChangeTime = 0; |
| 840 | |
| 841 | info_buf.CreationTime = 0; /* don't change */ |
| 842 | info_buf.Attributes = cpu_to_le32(dosattr); |
| 843 | |
| 844 | /* |
| 845 | * If the file is already open for write, just use that fileid |
| 846 | */ |
| 847 | open_file = find_writable_file(cifsInode); |
| 848 | if (open_file) { |
| 849 | netfid = open_file->netfid; |
| 850 | netpid = open_file->pid; |
| 851 | goto set_via_filehandle; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * NT4 apparently returns success on this call, but it doesn't |
| 856 | * really work. |
| 857 | */ |
| 858 | if (!(pTcon->ses->flags & CIFS_SES_NT4)) { |
| 859 | rc = CIFSSMBSetPathInfo(xid, pTcon, full_path, |
| 860 | &info_buf, cifs_sb->local_nls, |
| 861 | cifs_sb->mnt_cifs_flags & |
| 862 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Jeff Layton | 6b37faa | 2008-10-06 21:54:41 +0000 | [diff] [blame] | 863 | if (rc == 0) { |
| 864 | cifsInode->cifsAttrs = dosattr; |
| 865 | goto out; |
| 866 | } else if (rc != -EOPNOTSUPP && rc != -EINVAL) |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 867 | goto out; |
| 868 | } |
| 869 | |
| 870 | cFYI(1, ("calling SetFileInfo since SetPathInfo for " |
| 871 | "times not supported by this server")); |
| 872 | rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, |
| 873 | SYNCHRONIZE | FILE_WRITE_ATTRIBUTES, |
| 874 | CREATE_NOT_DIR, &netfid, &oplock, |
| 875 | NULL, cifs_sb->local_nls, |
| 876 | cifs_sb->mnt_cifs_flags & |
| 877 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 878 | |
| 879 | if (rc != 0) { |
| 880 | if (rc == -EIO) |
| 881 | rc = -EINVAL; |
| 882 | goto out; |
| 883 | } |
| 884 | |
| 885 | netpid = current->tgid; |
| 886 | |
| 887 | set_via_filehandle: |
| 888 | rc = CIFSSMBSetFileInfo(xid, pTcon, &info_buf, netfid, netpid); |
Steve French | d388908 | 2008-09-24 19:22:52 +0000 | [diff] [blame] | 889 | if (!rc) |
| 890 | cifsInode->cifsAttrs = dosattr; |
| 891 | |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 892 | if (open_file == NULL) |
| 893 | CIFSSMBClose(xid, pTcon, netfid); |
| 894 | else |
| 895 | atomic_dec(&open_file->wrtPending); |
| 896 | out: |
| 897 | return rc; |
| 898 | } |
| 899 | |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 900 | /* |
| 901 | * open the given file (if it isn't already), set the DELETE_ON_CLOSE bit |
| 902 | * and rename it to a random name that hopefully won't conflict with |
| 903 | * anything else. |
| 904 | */ |
| 905 | static int |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 906 | cifs_rename_pending_delete(char *full_path, struct dentry *dentry, int xid) |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 907 | { |
| 908 | int oplock = 0; |
| 909 | int rc; |
| 910 | __u16 netfid; |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 911 | struct inode *inode = dentry->d_inode; |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 912 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 913 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 914 | struct cifsTconInfo *tcon = cifs_sb->tcon; |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 915 | __u32 dosattr, origattr; |
| 916 | FILE_BASIC_INFO *info_buf = NULL; |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 917 | |
| 918 | rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN, |
Jeff Layton | dd1db2d | 2008-10-16 19:27:12 -0400 | [diff] [blame] | 919 | DELETE|FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR, |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 920 | &netfid, &oplock, NULL, cifs_sb->local_nls, |
| 921 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 922 | if (rc != 0) |
| 923 | goto out; |
| 924 | |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 925 | origattr = cifsInode->cifsAttrs; |
| 926 | if (origattr == 0) |
| 927 | origattr |= ATTR_NORMAL; |
| 928 | |
| 929 | dosattr = origattr & ~ATTR_READONLY; |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 930 | if (dosattr == 0) |
| 931 | dosattr |= ATTR_NORMAL; |
| 932 | dosattr |= ATTR_HIDDEN; |
| 933 | |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 934 | /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */ |
| 935 | if (dosattr != origattr) { |
| 936 | info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL); |
| 937 | if (info_buf == NULL) { |
| 938 | rc = -ENOMEM; |
| 939 | goto out_close; |
| 940 | } |
| 941 | info_buf->Attributes = cpu_to_le32(dosattr); |
| 942 | rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid, |
| 943 | current->tgid); |
| 944 | /* although we would like to mark the file hidden |
| 945 | if that fails we will still try to rename it */ |
Steve French | 4134609 | 2008-10-20 18:24:42 +0000 | [diff] [blame] | 946 | if (rc != 0) |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 947 | cifsInode->cifsAttrs = dosattr; |
| 948 | else |
| 949 | dosattr = origattr; /* since not able to change them */ |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 950 | } |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 951 | |
Jeff Layton | dd1db2d | 2008-10-16 19:27:12 -0400 | [diff] [blame] | 952 | /* rename the file */ |
| 953 | rc = CIFSSMBRenameOpenFile(xid, tcon, netfid, NULL, cifs_sb->local_nls, |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 954 | cifs_sb->mnt_cifs_flags & |
| 955 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 956 | if (rc != 0) { |
| 957 | rc = -ETXTBSY; |
| 958 | goto undo_setattr; |
| 959 | } |
Jeff Layton | 6d22f09 | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 960 | |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 961 | /* try to set DELETE_ON_CLOSE */ |
| 962 | if (!cifsInode->delete_pending) { |
| 963 | rc = CIFSSMBSetFileDisposition(xid, tcon, true, netfid, |
| 964 | current->tgid); |
| 965 | /* |
| 966 | * some samba versions return -ENOENT when we try to set the |
| 967 | * file disposition here. Likely a samba bug, but work around |
| 968 | * it for now. This means that some cifsXXX files may hang |
| 969 | * around after they shouldn't. |
| 970 | * |
| 971 | * BB: remove this hack after more servers have the fix |
| 972 | */ |
| 973 | if (rc == -ENOENT) |
| 974 | rc = 0; |
| 975 | else if (rc != 0) { |
| 976 | rc = -ETXTBSY; |
| 977 | goto undo_rename; |
| 978 | } |
| 979 | cifsInode->delete_pending = true; |
| 980 | } |
Jeff Layton | 7ce86d5 | 2008-09-24 11:32:59 -0400 | [diff] [blame] | 981 | |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 982 | out_close: |
| 983 | CIFSSMBClose(xid, tcon, netfid); |
| 984 | out: |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 985 | kfree(info_buf); |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 986 | return rc; |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 987 | |
| 988 | /* |
| 989 | * reset everything back to the original state. Don't bother |
| 990 | * dealing with errors here since we can't do anything about |
| 991 | * them anyway. |
| 992 | */ |
| 993 | undo_rename: |
| 994 | CIFSSMBRenameOpenFile(xid, tcon, netfid, dentry->d_name.name, |
| 995 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
| 996 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 997 | undo_setattr: |
| 998 | if (dosattr != origattr) { |
| 999 | info_buf->Attributes = cpu_to_le32(origattr); |
| 1000 | if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid, |
| 1001 | current->tgid)) |
| 1002 | cifsInode->cifsAttrs = origattr; |
| 1003 | } |
| 1004 | |
| 1005 | goto out_close; |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1006 | } |
| 1007 | |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1008 | |
| 1009 | /* |
| 1010 | * If dentry->d_inode is null (usually meaning the cached dentry |
| 1011 | * is a negative dentry) then we would attempt a standard SMB delete, but |
| 1012 | * if that fails we can not attempt the fall back mechanisms on EACESS |
| 1013 | * but will return the EACESS to the caller. Note that the VFS does not call |
| 1014 | * unlink on negative dentries currently. |
| 1015 | */ |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1016 | int cifs_unlink(struct inode *dir, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | { |
| 1018 | int rc = 0; |
| 1019 | int xid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | char *full_path = NULL; |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1021 | struct inode *inode = dentry->d_inode; |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1022 | struct cifsInodeInfo *cifs_inode; |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1023 | struct super_block *sb = dir->i_sb; |
| 1024 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); |
| 1025 | struct cifsTconInfo *tcon = cifs_sb->tcon; |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1026 | struct iattr *attrs = NULL; |
| 1027 | __u32 dosattr = 0, origattr = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1029 | cFYI(1, ("cifs_unlink, dir=0x%p, dentry=0x%p", dir, dentry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | |
| 1031 | xid = GetXid(); |
| 1032 | |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1033 | /* Unlink can be called from rename so we can not take the |
| 1034 | * sb->s_vfs_rename_mutex here */ |
| 1035 | full_path = build_path_from_dentry(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1037 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | FreeXid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1039 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | } |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 1041 | |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1042 | if ((tcon->ses->capabilities & CAP_UNIX) && |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 1043 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1044 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
| 1045 | rc = CIFSPOSIXDelFile(xid, tcon, full_path, |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 1046 | SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls, |
| 1047 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1048 | cFYI(1, ("posix del rc %d", rc)); |
| 1049 | if ((rc == 0) || (rc == -ENOENT)) |
| 1050 | goto psx_del_no_retry; |
| 1051 | } |
| 1052 | |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1053 | retry_std_delete: |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1054 | rc = CIFSSMBDelFile(xid, tcon, full_path, cifs_sb->local_nls, |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 1055 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1056 | |
Steve French | 2d785a5 | 2007-07-15 01:48:57 +0000 | [diff] [blame] | 1057 | psx_del_no_retry: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | if (!rc) { |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1059 | if (inode) |
| 1060 | drop_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | } else if (rc == -ENOENT) { |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1062 | d_drop(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | } else if (rc == -ETXTBSY) { |
Steve French | 3270958 | 2008-10-20 00:44:19 +0000 | [diff] [blame] | 1064 | rc = cifs_rename_pending_delete(full_path, dentry, xid); |
Jeff Layton | a12a1ac | 2008-09-23 11:48:35 -0400 | [diff] [blame] | 1065 | if (rc == 0) |
| 1066 | drop_nlink(inode); |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1067 | } else if ((rc == -EACCES) && (dosattr == 0) && inode) { |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1068 | attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); |
| 1069 | if (attrs == NULL) { |
| 1070 | rc = -ENOMEM; |
| 1071 | goto out_reval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | } |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1073 | |
| 1074 | /* try to reset dos attributes */ |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1075 | cifs_inode = CIFS_I(inode); |
| 1076 | origattr = cifs_inode->cifsAttrs; |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1077 | if (origattr == 0) |
| 1078 | origattr |= ATTR_NORMAL; |
| 1079 | dosattr = origattr & ~ATTR_READONLY; |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1080 | if (dosattr == 0) |
| 1081 | dosattr |= ATTR_NORMAL; |
| 1082 | dosattr |= ATTR_HIDDEN; |
| 1083 | |
| 1084 | rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr); |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1085 | if (rc != 0) |
| 1086 | goto out_reval; |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1087 | |
| 1088 | goto retry_std_delete; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | } |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1090 | |
| 1091 | /* undo the setattr if we errored out and it's needed */ |
| 1092 | if (rc != 0 && dosattr != 0) |
| 1093 | cifs_set_file_info(inode, attrs, xid, full_path, origattr); |
| 1094 | |
Steve French | 388e57b | 2008-09-16 23:50:58 +0000 | [diff] [blame] | 1095 | out_reval: |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1096 | if (inode) { |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1097 | cifs_inode = CIFS_I(inode); |
| 1098 | cifs_inode->time = 0; /* will force revalidate to get info |
Steve French | b2aeb9d | 2005-05-17 13:16:18 -0500 | [diff] [blame] | 1099 | when needed */ |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1100 | inode->i_ctime = current_fs_time(sb); |
Steve French | b2aeb9d | 2005-05-17 13:16:18 -0500 | [diff] [blame] | 1101 | } |
Jeff Layton | 5f0319a | 2008-09-16 14:05:16 -0400 | [diff] [blame] | 1102 | dir->i_ctime = dir->i_mtime = current_fs_time(sb); |
Steve French | ff69452 | 2009-04-20 19:45:13 +0000 | [diff] [blame] | 1103 | cifs_inode = CIFS_I(dir); |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1104 | CIFS_I(dir)->time = 0; /* force revalidate of dir as well */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | |
| 1106 | kfree(full_path); |
Steve French | 6050247 | 2008-10-07 18:42:52 +0000 | [diff] [blame] | 1107 | kfree(attrs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | FreeXid(xid); |
| 1109 | return rc; |
| 1110 | } |
| 1111 | |
| 1112 | int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode) |
| 1113 | { |
Jeff Layton | 6b37faa | 2008-10-06 21:54:41 +0000 | [diff] [blame] | 1114 | int rc = 0, tmprc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | int xid; |
| 1116 | struct cifs_sb_info *cifs_sb; |
| 1117 | struct cifsTconInfo *pTcon; |
| 1118 | char *full_path = NULL; |
| 1119 | struct inode *newinode = NULL; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 1120 | struct cifs_fattr fattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 1122 | cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | |
| 1124 | xid = GetXid(); |
| 1125 | |
| 1126 | cifs_sb = CIFS_SB(inode->i_sb); |
| 1127 | pTcon = cifs_sb->tcon; |
| 1128 | |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 1129 | full_path = build_path_from_dentry(direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1131 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | FreeXid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1133 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1135 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1136 | if ((pTcon->ses->capabilities & CAP_UNIX) && |
| 1137 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1138 | le64_to_cpu(pTcon->fsUnixInfo.Capability))) { |
| 1139 | u32 oplock = 0; |
Steve French | f6d0998 | 2008-01-08 23:18:22 +0000 | [diff] [blame] | 1140 | FILE_UNIX_BASIC_INFO *pInfo = |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1141 | kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1142 | if (pInfo == NULL) { |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1143 | rc = -ENOMEM; |
| 1144 | goto mkdir_out; |
| 1145 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1146 | |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 1147 | mode &= ~current_umask(); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1148 | rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT, |
| 1149 | mode, NULL /* netfid */, pInfo, &oplock, |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1150 | full_path, cifs_sb->local_nls, |
| 1151 | cifs_sb->mnt_cifs_flags & |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1152 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | c45d707 | 2007-09-17 02:04:21 +0000 | [diff] [blame] | 1153 | if (rc == -EOPNOTSUPP) { |
| 1154 | kfree(pInfo); |
| 1155 | goto mkdir_retry_old; |
| 1156 | } else if (rc) { |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1157 | cFYI(1, ("posix mkdir returned 0x%x", rc)); |
| 1158 | d_drop(direntry); |
| 1159 | } else { |
Cyril Gorcunov | 8f2376a | 2007-10-14 17:58:43 +0000 | [diff] [blame] | 1160 | if (pInfo->Type == cpu_to_le32(-1)) { |
| 1161 | /* no return info, go query for it */ |
Steve French | 5a07cdf | 2007-09-16 23:12:47 +0000 | [diff] [blame] | 1162 | kfree(pInfo); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1163 | goto mkdir_get_info; |
Steve French | 5a07cdf | 2007-09-16 23:12:47 +0000 | [diff] [blame] | 1164 | } |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1165 | /*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need |
| 1166 | to set uid/gid */ |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1167 | inc_nlink(inode); |
| 1168 | if (pTcon->nocase) |
| 1169 | direntry->d_op = &cifs_ci_dentry_ops; |
| 1170 | else |
| 1171 | direntry->d_op = &cifs_dentry_ops; |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1172 | |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 1173 | cifs_unix_basic_to_fattr(&fattr, pInfo, cifs_sb); |
| 1174 | newinode = cifs_iget(inode->i_sb, &fattr); |
| 1175 | if (!newinode) { |
Steve French | 5a07cdf | 2007-09-16 23:12:47 +0000 | [diff] [blame] | 1176 | kfree(pInfo); |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1177 | goto mkdir_get_info; |
Steve French | 5a07cdf | 2007-09-16 23:12:47 +0000 | [diff] [blame] | 1178 | } |
Jeff Layton | 6b37faa | 2008-10-06 21:54:41 +0000 | [diff] [blame] | 1179 | |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1180 | d_instantiate(direntry, newinode); |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1181 | |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1182 | #ifdef CONFIG_CIFS_DEBUG2 |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1183 | cFYI(1, ("instantiated dentry %p %s to inode %p", |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1184 | direntry, direntry->d_name.name, newinode)); |
| 1185 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1186 | if (newinode->i_nlink != 2) |
| 1187 | cFYI(1, ("unexpected number of links %d", |
Steve French | cbac3cb | 2007-04-25 11:46:06 +0000 | [diff] [blame] | 1188 | newinode->i_nlink)); |
| 1189 | #endif |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1190 | } |
| 1191 | kfree(pInfo); |
| 1192 | goto mkdir_out; |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1193 | } |
Steve French | c45d707 | 2007-09-17 02:04:21 +0000 | [diff] [blame] | 1194 | mkdir_retry_old: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | /* BB add setting the equivalent of mode via CreateX w/ACLs */ |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 1196 | rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls, |
| 1197 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | if (rc) { |
Steve French | 26a21b9 | 2006-05-31 18:05:34 +0000 | [diff] [blame] | 1199 | cFYI(1, ("cifs_mkdir returned 0x%x", rc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1200 | d_drop(direntry); |
| 1201 | } else { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1202 | mkdir_get_info: |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 1203 | inc_nlink(inode); |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 1204 | if (pTcon->unix_ext) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | rc = cifs_get_inode_info_unix(&newinode, full_path, |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1206 | inode->i_sb, xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | else |
| 1208 | rc = cifs_get_inode_info(&newinode, full_path, NULL, |
Steve French | 8b1327f | 2008-03-14 22:37:16 +0000 | [diff] [blame] | 1209 | inode->i_sb, xid, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | |
Steve French | b92327f | 2005-08-22 20:09:43 -0700 | [diff] [blame] | 1211 | if (pTcon->nocase) |
| 1212 | direntry->d_op = &cifs_ci_dentry_ops; |
| 1213 | else |
| 1214 | direntry->d_op = &cifs_dentry_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | d_instantiate(direntry, newinode); |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1216 | /* setting nlink not necessary except in cases where we |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1217 | * failed to get it from the server or was set bogus */ |
Steve French | 2dd29d3 | 2007-04-23 22:07:35 +0000 | [diff] [blame] | 1218 | if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2)) |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1219 | direntry->d_inode->i_nlink = 2; |
Jeff Layton | 9508991 | 2008-08-06 04:39:02 +0000 | [diff] [blame] | 1220 | |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 1221 | mode &= ~current_umask(); |
Jeff Layton | 9508991 | 2008-08-06 04:39:02 +0000 | [diff] [blame] | 1222 | /* must turn on setgid bit if parent dir has it */ |
| 1223 | if (inode->i_mode & S_ISGID) |
| 1224 | mode |= S_ISGID; |
| 1225 | |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 1226 | if (pTcon->unix_ext) { |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1227 | struct cifs_unix_set_info_args args = { |
| 1228 | .mode = mode, |
| 1229 | .ctime = NO_CHANGE_64, |
| 1230 | .atime = NO_CHANGE_64, |
| 1231 | .mtime = NO_CHANGE_64, |
| 1232 | .device = 0, |
| 1233 | }; |
Steve French | d0d2f2d | 2005-06-02 15:12:36 -0700 | [diff] [blame] | 1234 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
David Howells | a001e5b | 2008-11-14 10:38:47 +1100 | [diff] [blame] | 1235 | args.uid = (__u64)current_fsuid(); |
Jeff Layton | 9508991 | 2008-08-06 04:39:02 +0000 | [diff] [blame] | 1236 | if (inode->i_mode & S_ISGID) |
| 1237 | args.gid = (__u64)inode->i_gid; |
| 1238 | else |
David Howells | a001e5b | 2008-11-14 10:38:47 +1100 | [diff] [blame] | 1239 | args.gid = (__u64)current_fsgid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | } else { |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1241 | args.uid = NO_CHANGE_64; |
| 1242 | args.gid = NO_CHANGE_64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | } |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1244 | CIFSSMBUnixSetInfo(xid, pTcon, full_path, &args, |
| 1245 | cifs_sb->local_nls, |
| 1246 | cifs_sb->mnt_cifs_flags & |
| 1247 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Steve French | 3ce53fc | 2007-06-08 14:55:14 +0000 | [diff] [blame] | 1248 | } else { |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1249 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) && |
| 1250 | (mode & S_IWUGO) == 0) { |
| 1251 | FILE_BASIC_INFO pInfo; |
Jeff Layton | 6b37faa | 2008-10-06 21:54:41 +0000 | [diff] [blame] | 1252 | struct cifsInodeInfo *cifsInode; |
| 1253 | u32 dosattrs; |
| 1254 | |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1255 | memset(&pInfo, 0, sizeof(pInfo)); |
Jeff Layton | 6b37faa | 2008-10-06 21:54:41 +0000 | [diff] [blame] | 1256 | cifsInode = CIFS_I(newinode); |
| 1257 | dosattrs = cifsInode->cifsAttrs|ATTR_READONLY; |
| 1258 | pInfo.Attributes = cpu_to_le32(dosattrs); |
| 1259 | tmprc = CIFSSMBSetPathInfo(xid, pTcon, |
| 1260 | full_path, &pInfo, |
| 1261 | cifs_sb->local_nls, |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1262 | cifs_sb->mnt_cifs_flags & |
| 1263 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Jeff Layton | 6b37faa | 2008-10-06 21:54:41 +0000 | [diff] [blame] | 1264 | if (tmprc == 0) |
| 1265 | cifsInode->cifsAttrs = dosattrs; |
Jeff Layton | 67750fb | 2008-05-09 22:28:02 +0000 | [diff] [blame] | 1266 | } |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1267 | if (direntry->d_inode) { |
Jeff Layton | b0fd30d | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1268 | if (cifs_sb->mnt_cifs_flags & |
| 1269 | CIFS_MOUNT_DYNPERM) |
| 1270 | direntry->d_inode->i_mode = |
| 1271 | (mode | S_IFDIR); |
Steve French | 4e94a10 | 2008-05-23 18:22:46 +0000 | [diff] [blame] | 1272 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1273 | if (cifs_sb->mnt_cifs_flags & |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 1274 | CIFS_MOUNT_SET_UID) { |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1275 | direntry->d_inode->i_uid = |
David Howells | a001e5b | 2008-11-14 10:38:47 +1100 | [diff] [blame] | 1276 | current_fsuid(); |
Jeff Layton | 9508991 | 2008-08-06 04:39:02 +0000 | [diff] [blame] | 1277 | if (inode->i_mode & S_ISGID) |
| 1278 | direntry->d_inode->i_gid = |
| 1279 | inode->i_gid; |
| 1280 | else |
| 1281 | direntry->d_inode->i_gid = |
David Howells | a001e5b | 2008-11-14 10:38:47 +1100 | [diff] [blame] | 1282 | current_fsgid(); |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 1283 | } |
| 1284 | } |
Steve French | 2a138ebb | 2005-11-29 21:22:19 -0800 | [diff] [blame] | 1285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | } |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1287 | mkdir_out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | kfree(full_path); |
| 1289 | FreeXid(xid); |
| 1290 | return rc; |
| 1291 | } |
| 1292 | |
| 1293 | int cifs_rmdir(struct inode *inode, struct dentry *direntry) |
| 1294 | { |
| 1295 | int rc = 0; |
| 1296 | int xid; |
| 1297 | struct cifs_sb_info *cifs_sb; |
| 1298 | struct cifsTconInfo *pTcon; |
| 1299 | char *full_path = NULL; |
| 1300 | struct cifsInodeInfo *cifsInode; |
| 1301 | |
Steve French | 26a21b9 | 2006-05-31 18:05:34 +0000 | [diff] [blame] | 1302 | cFYI(1, ("cifs_rmdir, inode = 0x%p", inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
| 1304 | xid = GetXid(); |
| 1305 | |
| 1306 | cifs_sb = CIFS_SB(inode->i_sb); |
| 1307 | pTcon = cifs_sb->tcon; |
| 1308 | |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 1309 | full_path = build_path_from_dentry(direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1311 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | FreeXid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1313 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
Steve French | 737b758 | 2005-04-28 22:41:06 -0700 | [diff] [blame] | 1316 | rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls, |
| 1317 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | |
| 1319 | if (!rc) { |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 1320 | drop_nlink(inode); |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1321 | spin_lock(&direntry->d_inode->i_lock); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1322 | i_size_write(direntry->d_inode, 0); |
Dave Hansen | ce71ec3 | 2006-09-30 23:29:06 -0700 | [diff] [blame] | 1323 | clear_nlink(direntry->d_inode); |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1324 | spin_unlock(&direntry->d_inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | cifsInode = CIFS_I(direntry->d_inode); |
| 1328 | cifsInode->time = 0; /* force revalidate to go get info when |
| 1329 | needed */ |
Steve French | 42c24544 | 2009-01-13 22:03:55 +0000 | [diff] [blame] | 1330 | |
| 1331 | cifsInode = CIFS_I(inode); |
| 1332 | cifsInode->time = 0; /* force revalidate to get parent dir info |
| 1333 | since cached search results now invalid */ |
| 1334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime = |
| 1336 | current_fs_time(inode->i_sb); |
| 1337 | |
| 1338 | kfree(full_path); |
| 1339 | FreeXid(xid); |
| 1340 | return rc; |
| 1341 | } |
| 1342 | |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1343 | static int |
| 1344 | cifs_do_rename(int xid, struct dentry *from_dentry, const char *fromPath, |
| 1345 | struct dentry *to_dentry, const char *toPath) |
| 1346 | { |
| 1347 | struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb); |
| 1348 | struct cifsTconInfo *pTcon = cifs_sb->tcon; |
| 1349 | __u16 srcfid; |
| 1350 | int oplock, rc; |
| 1351 | |
| 1352 | /* try path-based rename first */ |
| 1353 | rc = CIFSSMBRename(xid, pTcon, fromPath, toPath, cifs_sb->local_nls, |
| 1354 | cifs_sb->mnt_cifs_flags & |
| 1355 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1356 | |
| 1357 | /* |
| 1358 | * don't bother with rename by filehandle unless file is busy and |
| 1359 | * source Note that cross directory moves do not work with |
| 1360 | * rename by filehandle to various Windows servers. |
| 1361 | */ |
| 1362 | if (rc == 0 || rc != -ETXTBSY) |
| 1363 | return rc; |
| 1364 | |
| 1365 | /* open the file to be renamed -- we need DELETE perms */ |
| 1366 | rc = CIFSSMBOpen(xid, pTcon, fromPath, FILE_OPEN, DELETE, |
| 1367 | CREATE_NOT_DIR, &srcfid, &oplock, NULL, |
| 1368 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
| 1369 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1370 | |
| 1371 | if (rc == 0) { |
| 1372 | rc = CIFSSMBRenameOpenFile(xid, pTcon, srcfid, |
| 1373 | (const char *) to_dentry->d_name.name, |
| 1374 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
| 1375 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1376 | |
| 1377 | CIFSSMBClose(xid, pTcon, srcfid); |
| 1378 | } |
| 1379 | |
| 1380 | return rc; |
| 1381 | } |
| 1382 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1383 | int cifs_rename(struct inode *source_dir, struct dentry *source_dentry, |
| 1384 | struct inode *target_dir, struct dentry *target_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | { |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1386 | char *fromName = NULL; |
| 1387 | char *toName = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | struct cifs_sb_info *cifs_sb_source; |
| 1389 | struct cifs_sb_info *cifs_sb_target; |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1390 | struct cifsTconInfo *tcon; |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1391 | FILE_UNIX_BASIC_INFO *info_buf_source = NULL; |
| 1392 | FILE_UNIX_BASIC_INFO *info_buf_target; |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1393 | int xid, rc, tmprc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1395 | cifs_sb_target = CIFS_SB(target_dir->i_sb); |
| 1396 | cifs_sb_source = CIFS_SB(source_dir->i_sb); |
| 1397 | tcon = cifs_sb_source->tcon; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1399 | xid = GetXid(); |
| 1400 | |
| 1401 | /* |
| 1402 | * BB: this might be allowed if same server, but different share. |
| 1403 | * Consider adding support for this |
| 1404 | */ |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1405 | if (tcon != cifs_sb_target->tcon) { |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1406 | rc = -EXDEV; |
| 1407 | goto cifs_rename_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1410 | /* |
| 1411 | * we already have the rename sem so we do not need to |
| 1412 | * grab it again here to protect the path integrity |
| 1413 | */ |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1414 | fromName = build_path_from_dentry(source_dentry); |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1415 | if (fromName == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | rc = -ENOMEM; |
| 1417 | goto cifs_rename_exit; |
| 1418 | } |
| 1419 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1420 | toName = build_path_from_dentry(target_dentry); |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1421 | if (toName == NULL) { |
| 1422 | rc = -ENOMEM; |
| 1423 | goto cifs_rename_exit; |
| 1424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1426 | rc = cifs_do_rename(xid, source_dentry, fromName, |
| 1427 | target_dentry, toName); |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1428 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1429 | if (rc == -EEXIST && tcon->unix_ext) { |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1430 | /* |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1431 | * Are src and dst hardlinks of same inode? We can |
| 1432 | * only tell with unix extensions enabled |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1433 | */ |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1434 | info_buf_source = |
| 1435 | kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), |
| 1436 | GFP_KERNEL); |
| 1437 | if (info_buf_source == NULL) { |
| 1438 | rc = -ENOMEM; |
| 1439 | goto cifs_rename_exit; |
| 1440 | } |
| 1441 | |
| 1442 | info_buf_target = info_buf_source + 1; |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1443 | tmprc = CIFSSMBUnixQPathInfo(xid, tcon, fromName, |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1444 | info_buf_source, |
| 1445 | cifs_sb_source->local_nls, |
| 1446 | cifs_sb_source->mnt_cifs_flags & |
| 1447 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1448 | if (tmprc != 0) |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1449 | goto unlink_target; |
| 1450 | |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1451 | tmprc = CIFSSMBUnixQPathInfo(xid, tcon, |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1452 | toName, info_buf_target, |
| 1453 | cifs_sb_target->local_nls, |
| 1454 | /* remap based on source sb */ |
| 1455 | cifs_sb_source->mnt_cifs_flags & |
| 1456 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1457 | |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1458 | if (tmprc == 0 && (info_buf_source->UniqueId == |
Jeff Layton | ae6884a | 2008-11-03 14:05:08 -0500 | [diff] [blame] | 1459 | info_buf_target->UniqueId)) { |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1460 | /* same file, POSIX says that this is a noop */ |
Jeff Layton | ae6884a | 2008-11-03 14:05:08 -0500 | [diff] [blame] | 1461 | rc = 0; |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1462 | goto cifs_rename_exit; |
Jeff Layton | ae6884a | 2008-11-03 14:05:08 -0500 | [diff] [blame] | 1463 | } |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1464 | } /* else ... BB we could add the same check for Windows by |
| 1465 | checking the UniqueId via FILE_INTERNAL_INFO */ |
| 1466 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1467 | unlink_target: |
Jeff Layton | fc6f394 | 2009-04-17 11:45:30 -0400 | [diff] [blame] | 1468 | /* Try unlinking the target dentry if it's not negative */ |
| 1469 | if (target_dentry->d_inode && (rc == -EACCES || rc == -EEXIST)) { |
Jeff Layton | 8d281ef | 2008-10-22 13:57:01 -0400 | [diff] [blame] | 1470 | tmprc = cifs_unlink(target_dir, target_dentry); |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1471 | if (tmprc) |
| 1472 | goto cifs_rename_exit; |
| 1473 | |
Jeff Layton | 14121bd | 2008-10-20 14:45:22 -0400 | [diff] [blame] | 1474 | rc = cifs_do_rename(xid, source_dentry, fromName, |
| 1475 | target_dentry, toName); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | cifs_rename_exit: |
Steve French | ee2fd96 | 2008-09-23 18:23:33 +0000 | [diff] [blame] | 1479 | kfree(info_buf_source); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | kfree(fromName); |
| 1481 | kfree(toName); |
| 1482 | FreeXid(xid); |
| 1483 | return rc; |
| 1484 | } |
| 1485 | |
| 1486 | int cifs_revalidate(struct dentry *direntry) |
| 1487 | { |
| 1488 | int xid; |
Jeff Layton | cea2180 | 2007-11-20 23:19:03 +0000 | [diff] [blame] | 1489 | int rc = 0, wbrc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | char *full_path; |
| 1491 | struct cifs_sb_info *cifs_sb; |
| 1492 | struct cifsInodeInfo *cifsInode; |
| 1493 | loff_t local_size; |
| 1494 | struct timespec local_mtime; |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 1495 | bool invalidate_inode = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | |
| 1497 | if (direntry->d_inode == NULL) |
| 1498 | return -ENOENT; |
| 1499 | |
| 1500 | cifsInode = CIFS_I(direntry->d_inode); |
| 1501 | |
| 1502 | if (cifsInode == NULL) |
| 1503 | return -ENOENT; |
| 1504 | |
| 1505 | /* no sense revalidating inode info on file that no one can write */ |
| 1506 | if (CIFS_I(direntry->d_inode)->clientCanCacheRead) |
| 1507 | return rc; |
| 1508 | |
| 1509 | xid = GetXid(); |
| 1510 | |
| 1511 | cifs_sb = CIFS_SB(direntry->d_sb); |
| 1512 | |
| 1513 | /* can not safely grab the rename sem here if rename calls revalidate |
| 1514 | since that would deadlock */ |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 1515 | full_path = build_path_from_dentry(direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1517 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | FreeXid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1519 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | } |
| 1521 | cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld " |
| 1522 | "jiffies %ld", full_path, direntry->d_inode, |
| 1523 | direntry->d_inode->i_count.counter, direntry, |
| 1524 | direntry->d_time, jiffies)); |
| 1525 | |
| 1526 | if (cifsInode->time == 0) { |
| 1527 | /* was set to zero previously to force revalidate */ |
| 1528 | } else if (time_before(jiffies, cifsInode->time + HZ) && |
| 1529 | lookupCacheEnabled) { |
| 1530 | if ((S_ISREG(direntry->d_inode->i_mode) == 0) || |
| 1531 | (direntry->d_inode->i_nlink == 1)) { |
| 1532 | kfree(full_path); |
| 1533 | FreeXid(xid); |
| 1534 | return rc; |
| 1535 | } else { |
| 1536 | cFYI(1, ("Have to revalidate file due to hardlinks")); |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | /* save mtime and size */ |
| 1541 | local_mtime = direntry->d_inode->i_mtime; |
| 1542 | local_size = direntry->d_inode->i_size; |
| 1543 | |
Steve French | c18c842 | 2007-07-18 23:21:09 +0000 | [diff] [blame] | 1544 | if (cifs_sb->tcon->unix_ext) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path, |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1546 | direntry->d_sb, xid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | if (rc) { |
| 1548 | cFYI(1, ("error on getting revalidate info %d", rc)); |
| 1549 | /* if (rc != -ENOENT) |
| 1550 | rc = 0; */ /* BB should we cache info on |
| 1551 | certain errors? */ |
| 1552 | } |
| 1553 | } else { |
| 1554 | rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL, |
Steve French | 8b1327f | 2008-03-14 22:37:16 +0000 | [diff] [blame] | 1555 | direntry->d_sb, xid, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | if (rc) { |
| 1557 | cFYI(1, ("error on getting revalidate info %d", rc)); |
| 1558 | /* if (rc != -ENOENT) |
| 1559 | rc = 0; */ /* BB should we cache info on |
| 1560 | certain errors? */ |
| 1561 | } |
| 1562 | } |
| 1563 | /* should we remap certain errors, access denied?, to zero */ |
| 1564 | |
| 1565 | /* if not oplocked, we invalidate inode pages if mtime or file size |
| 1566 | had changed on server */ |
| 1567 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1568 | if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | (local_size == direntry->d_inode->i_size)) { |
| 1570 | cFYI(1, ("cifs_revalidate - inode unchanged")); |
| 1571 | } else { |
| 1572 | /* file may have changed on server */ |
| 1573 | if (cifsInode->clientCanCacheRead) { |
| 1574 | /* no need to invalidate inode pages since we were the |
| 1575 | only ones who could have modified the file and the |
| 1576 | server copy is staler than ours */ |
| 1577 | } else { |
Steve French | 4b18f2a | 2008-04-29 00:06:05 +0000 | [diff] [blame] | 1578 | invalidate_inode = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | /* can not grab this sem since kernel filesys locking documentation |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1583 | indicates i_mutex may be taken by the kernel on lookup and rename |
| 1584 | which could deadlock if we grab the i_mutex here as well */ |
| 1585 | /* mutex_lock(&direntry->d_inode->i_mutex);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | /* need to write out dirty pages here */ |
| 1587 | if (direntry->d_inode->i_mapping) { |
| 1588 | /* do we need to lock inode until after invalidate completes |
| 1589 | below? */ |
Jeff Layton | cea2180 | 2007-11-20 23:19:03 +0000 | [diff] [blame] | 1590 | wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping); |
| 1591 | if (wbrc) |
| 1592 | CIFS_I(direntry->d_inode)->write_behind_rc = wbrc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | } |
| 1594 | if (invalidate_inode) { |
Steve French | 3abb927 | 2005-11-28 08:16:13 -0800 | [diff] [blame] | 1595 | /* shrink_dcache not necessary now that cifs dentry ops |
| 1596 | are exported for negative dentries */ |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1597 | /* if (S_ISDIR(direntry->d_inode->i_mode)) |
Steve French | 3abb927 | 2005-11-28 08:16:13 -0800 | [diff] [blame] | 1598 | shrink_dcache_parent(direntry); */ |
| 1599 | if (S_ISREG(direntry->d_inode->i_mode)) { |
Suresh Jayaraman | 9e96af8 | 2008-08-05 14:38:40 +0530 | [diff] [blame] | 1600 | if (direntry->d_inode->i_mapping) { |
Jeff Layton | cea2180 | 2007-11-20 23:19:03 +0000 | [diff] [blame] | 1601 | wbrc = filemap_fdatawait(direntry->d_inode->i_mapping); |
| 1602 | if (wbrc) |
| 1603 | CIFS_I(direntry->d_inode)->write_behind_rc = wbrc; |
Suresh Jayaraman | 9e96af8 | 2008-08-05 14:38:40 +0530 | [diff] [blame] | 1604 | } |
Steve French | 3abb927 | 2005-11-28 08:16:13 -0800 | [diff] [blame] | 1605 | /* may eventually have to do this for open files too */ |
| 1606 | if (list_empty(&(cifsInode->openFileList))) { |
| 1607 | /* changed on server - flush read ahead pages */ |
| 1608 | cFYI(1, ("Invalidating read ahead data on " |
| 1609 | "closed file")); |
| 1610 | invalidate_remote_inode(direntry->d_inode); |
| 1611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | } |
| 1613 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 1614 | /* mutex_unlock(&direntry->d_inode->i_mutex); */ |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | kfree(full_path); |
| 1617 | FreeXid(xid); |
| 1618 | return rc; |
| 1619 | } |
| 1620 | |
| 1621 | int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 1622 | struct kstat *stat) |
| 1623 | { |
| 1624 | int err = cifs_revalidate(dentry); |
Steve French | 5fe14c8 | 2006-11-07 19:26:33 +0000 | [diff] [blame] | 1625 | if (!err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | generic_fillattr(dentry->d_inode, stat); |
Steve French | 5fe14c8 | 2006-11-07 19:26:33 +0000 | [diff] [blame] | 1627 | stat->blksize = CIFS_MAX_MSGSIZE; |
Jeff Layton | cc0bad7 | 2009-06-25 00:56:52 -0400 | [diff] [blame^] | 1628 | stat->ino = CIFS_I(dentry->d_inode)->uniqueid; |
Steve French | 5fe14c8 | 2006-11-07 19:26:33 +0000 | [diff] [blame] | 1629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | return err; |
| 1631 | } |
| 1632 | |
| 1633 | static int cifs_truncate_page(struct address_space *mapping, loff_t from) |
| 1634 | { |
| 1635 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 1636 | unsigned offset = from & (PAGE_CACHE_SIZE - 1); |
| 1637 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | int rc = 0; |
| 1639 | |
| 1640 | page = grab_cache_page(mapping, index); |
| 1641 | if (!page) |
| 1642 | return -ENOMEM; |
| 1643 | |
Christoph Lameter | eebd2aa | 2008-02-04 22:28:29 -0800 | [diff] [blame] | 1644 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | unlock_page(page); |
| 1646 | page_cache_release(page); |
| 1647 | return rc; |
| 1648 | } |
| 1649 | |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1650 | static int cifs_vmtruncate(struct inode *inode, loff_t offset) |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1651 | { |
| 1652 | struct address_space *mapping = inode->i_mapping; |
| 1653 | unsigned long limit; |
| 1654 | |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 1655 | spin_lock(&inode->i_lock); |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1656 | if (inode->i_size < offset) |
| 1657 | goto do_expand; |
| 1658 | /* |
| 1659 | * truncation of in-use swapfiles is disallowed - it would cause |
| 1660 | * subsequent swapout to scribble on the now-freed blocks. |
| 1661 | */ |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 1662 | if (IS_SWAPFILE(inode)) { |
| 1663 | spin_unlock(&inode->i_lock); |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1664 | goto out_busy; |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 1665 | } |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1666 | i_size_write(inode, offset); |
| 1667 | spin_unlock(&inode->i_lock); |
Steve French | 8064ab4 | 2007-08-22 22:12:07 +0000 | [diff] [blame] | 1668 | /* |
| 1669 | * unmap_mapping_range is called twice, first simply for efficiency |
| 1670 | * so that truncate_inode_pages does fewer single-page unmaps. However |
| 1671 | * after this first call, and before truncate_inode_pages finishes, |
| 1672 | * it is possible for private pages to be COWed, which remain after |
| 1673 | * truncate_inode_pages finishes, hence the second unmap_mapping_range |
| 1674 | * call must be made for correctness. |
| 1675 | */ |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1676 | unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); |
| 1677 | truncate_inode_pages(mapping, offset); |
Steve French | 8064ab4 | 2007-08-22 22:12:07 +0000 | [diff] [blame] | 1678 | unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1679 | goto out_truncate; |
| 1680 | |
| 1681 | do_expand: |
| 1682 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 1683 | if (limit != RLIM_INFINITY && offset > limit) { |
| 1684 | spin_unlock(&inode->i_lock); |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1685 | goto out_sig; |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 1686 | } |
| 1687 | if (offset > inode->i_sb->s_maxbytes) { |
| 1688 | spin_unlock(&inode->i_lock); |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1689 | goto out_big; |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 1690 | } |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1691 | i_size_write(inode, offset); |
Steve French | ba6a46a | 2007-02-26 20:06:29 +0000 | [diff] [blame] | 1692 | spin_unlock(&inode->i_lock); |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1693 | out_truncate: |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 1694 | if (inode->i_op->truncate) |
Steve French | 3677db1 | 2007-02-26 16:46:11 +0000 | [diff] [blame] | 1695 | inode->i_op->truncate(inode); |
| 1696 | return 0; |
| 1697 | out_sig: |
| 1698 | send_sig(SIGXFSZ, current, 0); |
| 1699 | out_big: |
| 1700 | return -EFBIG; |
| 1701 | out_busy: |
| 1702 | return -ETXTBSY; |
| 1703 | } |
| 1704 | |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 1705 | static int |
| 1706 | cifs_set_file_size(struct inode *inode, struct iattr *attrs, |
| 1707 | int xid, char *full_path) |
| 1708 | { |
| 1709 | int rc; |
| 1710 | struct cifsFileInfo *open_file; |
| 1711 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 1712 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 1713 | struct cifsTconInfo *pTcon = cifs_sb->tcon; |
| 1714 | |
| 1715 | /* |
| 1716 | * To avoid spurious oplock breaks from server, in the case of |
| 1717 | * inodes that we already have open, avoid doing path based |
| 1718 | * setting of file size if we can do it by handle. |
| 1719 | * This keeps our caching token (oplock) and avoids timeouts |
| 1720 | * when the local oplock break takes longer to flush |
| 1721 | * writebehind data than the SMB timeout for the SetPathInfo |
| 1722 | * request would allow |
| 1723 | */ |
| 1724 | open_file = find_writable_file(cifsInode); |
| 1725 | if (open_file) { |
| 1726 | __u16 nfid = open_file->netfid; |
| 1727 | __u32 npid = open_file->pid; |
| 1728 | rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size, nfid, |
| 1729 | npid, false); |
| 1730 | atomic_dec(&open_file->wrtPending); |
| 1731 | cFYI(1, ("SetFSize for attrs rc = %d", rc)); |
| 1732 | if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { |
| 1733 | unsigned int bytes_written; |
| 1734 | rc = CIFSSMBWrite(xid, pTcon, nfid, 0, attrs->ia_size, |
| 1735 | &bytes_written, NULL, NULL, 1); |
| 1736 | cFYI(1, ("Wrt seteof rc %d", rc)); |
| 1737 | } |
| 1738 | } else |
| 1739 | rc = -EINVAL; |
| 1740 | |
| 1741 | if (rc != 0) { |
| 1742 | /* Set file size by pathname rather than by handle |
| 1743 | either because no valid, writeable file handle for |
| 1744 | it was found or because there was an error setting |
| 1745 | it by handle */ |
| 1746 | rc = CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, |
| 1747 | false, cifs_sb->local_nls, |
| 1748 | cifs_sb->mnt_cifs_flags & |
| 1749 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1750 | cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc)); |
| 1751 | if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { |
| 1752 | __u16 netfid; |
| 1753 | int oplock = 0; |
| 1754 | |
| 1755 | rc = SMBLegacyOpen(xid, pTcon, full_path, |
| 1756 | FILE_OPEN, GENERIC_WRITE, |
| 1757 | CREATE_NOT_DIR, &netfid, &oplock, NULL, |
| 1758 | cifs_sb->local_nls, |
| 1759 | cifs_sb->mnt_cifs_flags & |
| 1760 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1761 | if (rc == 0) { |
| 1762 | unsigned int bytes_written; |
| 1763 | rc = CIFSSMBWrite(xid, pTcon, netfid, 0, |
| 1764 | attrs->ia_size, |
| 1765 | &bytes_written, NULL, |
| 1766 | NULL, 1); |
| 1767 | cFYI(1, ("wrt seteof rc %d", rc)); |
| 1768 | CIFSSMBClose(xid, pTcon, netfid); |
| 1769 | } |
| 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | if (rc == 0) { |
Jeff Layton | fbec9ab | 2009-04-03 13:44:00 -0400 | [diff] [blame] | 1774 | cifsInode->server_eof = attrs->ia_size; |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 1775 | rc = cifs_vmtruncate(inode, attrs->ia_size); |
| 1776 | cifs_truncate_page(inode->i_mapping, inode->i_size); |
| 1777 | } |
| 1778 | |
| 1779 | return rc; |
| 1780 | } |
| 1781 | |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1782 | static int |
| 1783 | cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs) |
| 1784 | { |
| 1785 | int rc; |
| 1786 | int xid; |
| 1787 | char *full_path = NULL; |
| 1788 | struct inode *inode = direntry->d_inode; |
| 1789 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
| 1790 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 1791 | struct cifsTconInfo *pTcon = cifs_sb->tcon; |
| 1792 | struct cifs_unix_set_info_args *args = NULL; |
| 1793 | |
| 1794 | cFYI(1, ("setattr_unix on file %s attrs->ia_valid=0x%x", |
| 1795 | direntry->d_name.name, attrs->ia_valid)); |
| 1796 | |
| 1797 | xid = GetXid(); |
| 1798 | |
| 1799 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) { |
| 1800 | /* check if we have permission to change attrs */ |
| 1801 | rc = inode_change_ok(inode, attrs); |
| 1802 | if (rc < 0) |
| 1803 | goto out; |
| 1804 | else |
| 1805 | rc = 0; |
| 1806 | } |
| 1807 | |
| 1808 | full_path = build_path_from_dentry(direntry); |
| 1809 | if (full_path == NULL) { |
| 1810 | rc = -ENOMEM; |
| 1811 | goto out; |
| 1812 | } |
| 1813 | |
Jeff Layton | 0f4d634 | 2009-03-26 13:35:37 -0400 | [diff] [blame] | 1814 | /* |
| 1815 | * Attempt to flush data before changing attributes. We need to do |
| 1816 | * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the |
| 1817 | * ownership or mode then we may also need to do this. Here, we take |
| 1818 | * the safe way out and just do the flush on all setattr requests. If |
| 1819 | * the flush returns error, store it to report later and continue. |
| 1820 | * |
| 1821 | * BB: This should be smarter. Why bother flushing pages that |
| 1822 | * will be truncated anyway? Also, should we error out here if |
| 1823 | * the flush returns error? |
| 1824 | */ |
| 1825 | rc = filemap_write_and_wait(inode->i_mapping); |
| 1826 | if (rc != 0) { |
| 1827 | cifsInode->write_behind_rc = rc; |
| 1828 | rc = 0; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | if (attrs->ia_valid & ATTR_SIZE) { |
| 1832 | rc = cifs_set_file_size(inode, attrs, xid, full_path); |
| 1833 | if (rc != 0) |
| 1834 | goto out; |
| 1835 | } |
| 1836 | |
| 1837 | /* skip mode change if it's just for clearing setuid/setgid */ |
| 1838 | if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 1839 | attrs->ia_valid &= ~ATTR_MODE; |
| 1840 | |
| 1841 | args = kmalloc(sizeof(*args), GFP_KERNEL); |
| 1842 | if (args == NULL) { |
| 1843 | rc = -ENOMEM; |
| 1844 | goto out; |
| 1845 | } |
| 1846 | |
| 1847 | /* set up the struct */ |
| 1848 | if (attrs->ia_valid & ATTR_MODE) |
| 1849 | args->mode = attrs->ia_mode; |
| 1850 | else |
| 1851 | args->mode = NO_CHANGE_64; |
| 1852 | |
| 1853 | if (attrs->ia_valid & ATTR_UID) |
| 1854 | args->uid = attrs->ia_uid; |
| 1855 | else |
| 1856 | args->uid = NO_CHANGE_64; |
| 1857 | |
| 1858 | if (attrs->ia_valid & ATTR_GID) |
| 1859 | args->gid = attrs->ia_gid; |
| 1860 | else |
| 1861 | args->gid = NO_CHANGE_64; |
| 1862 | |
| 1863 | if (attrs->ia_valid & ATTR_ATIME) |
| 1864 | args->atime = cifs_UnixTimeToNT(attrs->ia_atime); |
| 1865 | else |
| 1866 | args->atime = NO_CHANGE_64; |
| 1867 | |
| 1868 | if (attrs->ia_valid & ATTR_MTIME) |
| 1869 | args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime); |
| 1870 | else |
| 1871 | args->mtime = NO_CHANGE_64; |
| 1872 | |
| 1873 | if (attrs->ia_valid & ATTR_CTIME) |
| 1874 | args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime); |
| 1875 | else |
| 1876 | args->ctime = NO_CHANGE_64; |
| 1877 | |
| 1878 | args->device = 0; |
| 1879 | rc = CIFSSMBUnixSetInfo(xid, pTcon, full_path, args, |
| 1880 | cifs_sb->local_nls, |
| 1881 | cifs_sb->mnt_cifs_flags & |
| 1882 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
| 1883 | |
| 1884 | if (!rc) |
| 1885 | rc = inode_setattr(inode, attrs); |
| 1886 | out: |
| 1887 | kfree(args); |
| 1888 | kfree(full_path); |
| 1889 | FreeXid(xid); |
| 1890 | return rc; |
| 1891 | } |
| 1892 | |
Jeff Layton | 0510eeb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1893 | static int |
| 1894 | cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | { |
| 1896 | int xid; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1897 | struct inode *inode = direntry->d_inode; |
| 1898 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1899 | struct cifsInodeInfo *cifsInode = CIFS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | char *full_path = NULL; |
| 1901 | int rc = -EACCES; |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1902 | __u32 dosattr = 0; |
Jeff Layton | 4e1e7fb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1903 | __u64 mode = NO_CHANGE_64; |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1904 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | xid = GetXid(); |
| 1906 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1907 | cFYI(1, ("setattr on file %s attrs->iavalid 0x%x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | direntry->d_name.name, attrs->ia_valid)); |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 1909 | |
Steve French | 2a138ebb | 2005-11-29 21:22:19 -0800 | [diff] [blame] | 1910 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) { |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 1911 | /* check if we have permission to change attrs */ |
Jeff Layton | 02eadef | 2008-05-09 21:26:11 +0000 | [diff] [blame] | 1912 | rc = inode_change_ok(inode, attrs); |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 1913 | if (rc < 0) { |
Steve French | 6473a55 | 2005-11-29 20:20:10 -0800 | [diff] [blame] | 1914 | FreeXid(xid); |
| 1915 | return rc; |
| 1916 | } else |
| 1917 | rc = 0; |
| 1918 | } |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 1919 | |
Steve French | 7f57356 | 2005-08-30 11:32:14 -0700 | [diff] [blame] | 1920 | full_path = build_path_from_dentry(direntry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | if (full_path == NULL) { |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1922 | rc = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | FreeXid(xid); |
Suresh Jayaraman | 0f3bc09 | 2009-06-25 18:12:34 +0530 | [diff] [blame] | 1924 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | |
Jeff Layton | 0f4d634 | 2009-03-26 13:35:37 -0400 | [diff] [blame] | 1927 | /* |
| 1928 | * Attempt to flush data before changing attributes. We need to do |
| 1929 | * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the |
| 1930 | * ownership or mode then we may also need to do this. Here, we take |
| 1931 | * the safe way out and just do the flush on all setattr requests. If |
| 1932 | * the flush returns error, store it to report later and continue. |
| 1933 | * |
| 1934 | * BB: This should be smarter. Why bother flushing pages that |
| 1935 | * will be truncated anyway? Also, should we error out here if |
| 1936 | * the flush returns error? |
| 1937 | */ |
| 1938 | rc = filemap_write_and_wait(inode->i_mapping); |
| 1939 | if (rc != 0) { |
| 1940 | cifsInode->write_behind_rc = rc; |
| 1941 | rc = 0; |
Steve French | 5053144 | 2008-03-14 19:21:31 +0000 | [diff] [blame] | 1942 | } |
Jeff Layton | cea2180 | 2007-11-20 23:19:03 +0000 | [diff] [blame] | 1943 | |
Steve French | 5053144 | 2008-03-14 19:21:31 +0000 | [diff] [blame] | 1944 | if (attrs->ia_valid & ATTR_SIZE) { |
Jeff Layton | 8efdbde | 2008-07-23 21:28:12 +0000 | [diff] [blame] | 1945 | rc = cifs_set_file_size(inode, attrs, xid, full_path); |
| 1946 | if (rc != 0) |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 1947 | goto cifs_setattr_exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | } |
Jeff Layton | 4ca691a | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1949 | |
| 1950 | /* |
| 1951 | * Without unix extensions we can't send ownership changes to the |
| 1952 | * server, so silently ignore them. This is consistent with how |
| 1953 | * local DOS/Windows filesystems behave (VFAT, NTFS, etc). With |
| 1954 | * CIFSACL support + proper Windows to Unix idmapping, we may be |
| 1955 | * able to support this in the future. |
| 1956 | */ |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1957 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) |
Jeff Layton | 4ca691a | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1958 | attrs->ia_valid &= ~(ATTR_UID | ATTR_GID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | |
Jeff Layton | d32c4f2 | 2007-10-18 03:05:22 -0700 | [diff] [blame] | 1960 | /* skip mode change if it's just for clearing setuid/setgid */ |
| 1961 | if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 1962 | attrs->ia_valid &= ~ATTR_MODE; |
| 1963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | if (attrs->ia_valid & ATTR_MODE) { |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1965 | cFYI(1, ("Mode changed to 0%o", attrs->ia_mode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 | mode = attrs->ia_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | } |
| 1968 | |
Jeff Layton | 3fe5c1d | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1969 | if (attrs->ia_valid & ATTR_MODE) { |
Steve French | cdbce9c8 | 2005-11-19 21:04:52 -0800 | [diff] [blame] | 1970 | rc = 0; |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1971 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
| 1972 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) |
Jeff Layton | 02eadef | 2008-05-09 21:26:11 +0000 | [diff] [blame] | 1973 | rc = mode_to_acl(inode, full_path, mode); |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1974 | else |
Steve French | 9783758 | 2007-12-31 07:47:21 +0000 | [diff] [blame] | 1975 | #endif |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1976 | if (((mode & S_IWUGO) == 0) && |
| 1977 | (cifsInode->cifsAttrs & ATTR_READONLY) == 0) { |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1978 | |
| 1979 | dosattr = cifsInode->cifsAttrs | ATTR_READONLY; |
| 1980 | |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1981 | /* fix up mode if we're not using dynperm */ |
| 1982 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0) |
| 1983 | attrs->ia_mode = inode->i_mode & ~S_IWUGO; |
| 1984 | } else if ((mode & S_IWUGO) && |
| 1985 | (cifsInode->cifsAttrs & ATTR_READONLY)) { |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 1986 | |
| 1987 | dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY; |
| 1988 | /* Attributes of 0 are ignored */ |
| 1989 | if (dosattr == 0) |
| 1990 | dosattr |= ATTR_NORMAL; |
Jeff Layton | 5132861 | 2008-05-22 09:33:34 -0400 | [diff] [blame] | 1991 | |
| 1992 | /* reset local inode permissions to normal */ |
| 1993 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) { |
| 1994 | attrs->ia_mode &= ~(S_IALLUGO); |
| 1995 | if (S_ISDIR(inode->i_mode)) |
| 1996 | attrs->ia_mode |= |
| 1997 | cifs_sb->mnt_dir_mode; |
| 1998 | else |
| 1999 | attrs->ia_mode |= |
| 2000 | cifs_sb->mnt_file_mode; |
| 2001 | } |
| 2002 | } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) { |
| 2003 | /* ignore mode change - ATTR_READONLY hasn't changed */ |
| 2004 | attrs->ia_valid &= ~ATTR_MODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2008 | if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) || |
| 2009 | ((attrs->ia_valid & ATTR_MODE) && dosattr)) { |
| 2010 | rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr); |
| 2011 | /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 2013 | /* Even if error on time set, no sense failing the call if |
| 2014 | the server would set the time to a reasonable value anyway, |
| 2015 | and this check ensures that we are not being called from |
| 2016 | sys_utimes in which case we ought to fail the call back to |
| 2017 | the user when the server rejects the call */ |
Steve French | fb8c4b1 | 2007-07-10 01:16:18 +0000 | [diff] [blame] | 2018 | if ((rc) && (attrs->ia_valid & |
Jeff Layton | feb3e20 | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2019 | (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE))) |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 2020 | rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | } |
| 2022 | |
| 2023 | /* do not need local check to inode_check_ok since the server does |
| 2024 | that */ |
| 2025 | if (!rc) |
Jeff Layton | 02eadef | 2008-05-09 21:26:11 +0000 | [diff] [blame] | 2026 | rc = inode_setattr(inode, attrs); |
Steve French | e30dcf3 | 2005-09-20 20:49:16 -0700 | [diff] [blame] | 2027 | cifs_setattr_exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | kfree(full_path); |
| 2029 | FreeXid(xid); |
| 2030 | return rc; |
| 2031 | } |
| 2032 | |
Jeff Layton | 0510eeb | 2008-08-02 07:26:12 -0400 | [diff] [blame] | 2033 | int |
| 2034 | cifs_setattr(struct dentry *direntry, struct iattr *attrs) |
| 2035 | { |
| 2036 | struct inode *inode = direntry->d_inode; |
| 2037 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); |
| 2038 | struct cifsTconInfo *pTcon = cifs_sb->tcon; |
| 2039 | |
| 2040 | if (pTcon->unix_ext) |
| 2041 | return cifs_setattr_unix(direntry, attrs); |
| 2042 | |
| 2043 | return cifs_setattr_nounix(direntry, attrs); |
| 2044 | |
| 2045 | /* BB: add cifs_setattr_legacy for really old servers */ |
| 2046 | } |
| 2047 | |
Steve French | 99ee4db | 2007-02-27 05:35:17 +0000 | [diff] [blame] | 2048 | #if 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | void cifs_delete_inode(struct inode *inode) |
| 2050 | { |
Steve French | 26a21b9 | 2006-05-31 18:05:34 +0000 | [diff] [blame] | 2051 | cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | /* may have to add back in if and when safe distributed caching of |
| 2053 | directories added e.g. via FindNotify */ |
| 2054 | } |
Steve French | 99ee4db | 2007-02-27 05:35:17 +0000 | [diff] [blame] | 2055 | #endif |