| 1 | // SPDX-License-Identifier: LGPL-2.1 |
| 2 | /* |
| 3 | * |
| 4 | * vfs operations that deal with dentries |
| 5 | * |
| 6 | * Copyright (C) International Business Machines Corp., 2002,2009 |
| 7 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 8 | * |
| 9 | */ |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/stat.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/namei.h> |
| 14 | #include <linux/mount.h> |
| 15 | #include <linux/file.h> |
| 16 | #include "cifsfs.h" |
| 17 | #include "cifspdu.h" |
| 18 | #include "cifsglob.h" |
| 19 | #include "cifsproto.h" |
| 20 | #include "cifs_debug.h" |
| 21 | #include "cifs_fs_sb.h" |
| 22 | #include "cifs_unicode.h" |
| 23 | #include "fs_context.h" |
| 24 | #include "cifs_ioctl.h" |
| 25 | #include "fscache.h" |
| 26 | #include "cached_dir.h" |
| 27 | |
| 28 | static void |
| 29 | renew_parental_timestamps(struct dentry *direntry) |
| 30 | { |
| 31 | /* BB check if there is a way to get the kernel to do this or if we |
| 32 | really need this */ |
| 33 | do { |
| 34 | cifs_set_time(dentry: direntry, time: jiffies); |
| 35 | direntry = direntry->d_parent; |
| 36 | } while (!IS_ROOT(direntry)); |
| 37 | } |
| 38 | |
| 39 | char * |
| 40 | cifs_build_path_to_root(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb, |
| 41 | struct cifs_tcon *tcon, int add_treename) |
| 42 | { |
| 43 | int pplen = ctx->prepath ? strlen(ctx->prepath) + 1 : 0; |
| 44 | int dfsplen; |
| 45 | char *full_path = NULL; |
| 46 | |
| 47 | /* if no prefix path, simply set path to the root of share to "" */ |
| 48 | if (pplen == 0) { |
| 49 | full_path = kzalloc(1, GFP_KERNEL); |
| 50 | return full_path; |
| 51 | } |
| 52 | |
| 53 | if (add_treename) |
| 54 | dfsplen = strnlen(p: tcon->tree_name, MAX_TREE_SIZE + 1); |
| 55 | else |
| 56 | dfsplen = 0; |
| 57 | |
| 58 | full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL); |
| 59 | if (full_path == NULL) |
| 60 | return full_path; |
| 61 | |
| 62 | if (dfsplen) |
| 63 | memcpy(full_path, tcon->tree_name, dfsplen); |
| 64 | full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb); |
| 65 | memcpy(full_path + dfsplen + 1, ctx->prepath, pplen); |
| 66 | convert_delimiter(path: full_path, delim: CIFS_DIR_SEP(cifs_sb)); |
| 67 | return full_path; |
| 68 | } |
| 69 | |
| 70 | /* Note: caller must free return buffer */ |
| 71 | const char * |
| 72 | build_path_from_dentry(struct dentry *direntry, void *page) |
| 73 | { |
| 74 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb: direntry->d_sb); |
| 75 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
| 76 | bool prefix = tcon->Flags & SMB_SHARE_IS_IN_DFS; |
| 77 | |
| 78 | return build_path_from_dentry_optional_prefix(direntry, page, |
| 79 | prefix); |
| 80 | } |
| 81 | |
| 82 | char *__build_path_from_dentry_optional_prefix(struct dentry *direntry, void *page, |
| 83 | const char *tree, int tree_len, |
| 84 | bool prefix) |
| 85 | { |
| 86 | int dfsplen; |
| 87 | int pplen = 0; |
| 88 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb: direntry->d_sb); |
| 89 | char dirsep = CIFS_DIR_SEP(cifs_sb); |
| 90 | char *s; |
| 91 | |
| 92 | if (unlikely(!page)) |
| 93 | return ERR_PTR(error: -ENOMEM); |
| 94 | |
| 95 | if (prefix) |
| 96 | dfsplen = strnlen(p: tree, maxlen: tree_len + 1); |
| 97 | else |
| 98 | dfsplen = 0; |
| 99 | |
| 100 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) |
| 101 | pplen = cifs_sb->prepath ? strlen(cifs_sb->prepath) + 1 : 0; |
| 102 | |
| 103 | s = dentry_path_raw(direntry, page, PATH_MAX); |
| 104 | if (IS_ERR(ptr: s)) |
| 105 | return s; |
| 106 | if (!s[1]) // for root we want "", not "/" |
| 107 | s++; |
| 108 | if (s < (char *)page + pplen + dfsplen) |
| 109 | return ERR_PTR(error: -ENAMETOOLONG); |
| 110 | if (pplen) { |
| 111 | cifs_dbg(FYI, "using cifs_sb prepath <%s>\n" , cifs_sb->prepath); |
| 112 | s -= pplen; |
| 113 | memcpy(s + 1, cifs_sb->prepath, pplen - 1); |
| 114 | *s = '/'; |
| 115 | } |
| 116 | if (dirsep != '/') { |
| 117 | /* BB test paths to Windows with '/' in the midst of prepath */ |
| 118 | char *p; |
| 119 | |
| 120 | for (p = s; *p; p++) |
| 121 | if (*p == '/') |
| 122 | *p = dirsep; |
| 123 | } |
| 124 | if (dfsplen) { |
| 125 | s -= dfsplen; |
| 126 | memcpy(s, tree, dfsplen); |
| 127 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) { |
| 128 | int i; |
| 129 | for (i = 0; i < dfsplen; i++) { |
| 130 | if (s[i] == '\\') |
| 131 | s[i] = '/'; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | return s; |
| 136 | } |
| 137 | |
| 138 | char *build_path_from_dentry_optional_prefix(struct dentry *direntry, void *page, |
| 139 | bool prefix) |
| 140 | { |
| 141 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb: direntry->d_sb); |
| 142 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
| 143 | |
| 144 | return __build_path_from_dentry_optional_prefix(direntry, page, tree: tcon->tree_name, |
| 145 | MAX_TREE_SIZE, prefix); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Don't allow path components longer than the server max. |
| 150 | * Don't allow the separator character in a path component. |
| 151 | * The VFS will not allow "/", but "\" is allowed by posix. |
| 152 | */ |
| 153 | static int |
| 154 | check_name(struct dentry *direntry, struct cifs_tcon *tcon) |
| 155 | { |
| 156 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb: direntry->d_sb); |
| 157 | int i; |
| 158 | |
| 159 | if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength && |
| 160 | direntry->d_name.len > |
| 161 | le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength))) |
| 162 | return -ENAMETOOLONG; |
| 163 | |
| 164 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) { |
| 165 | for (i = 0; i < direntry->d_name.len; i++) { |
| 166 | if (direntry->d_name.name[i] == '\\') { |
| 167 | cifs_dbg(FYI, "Invalid file name\n" ); |
| 168 | return -EINVAL; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* Inode operations in similar order to how they appear in Linux file fs.h */ |
| 177 | |
| 178 | static int cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid, |
| 179 | struct tcon_link *tlink, unsigned int oflags, umode_t mode, __u32 *oplock, |
| 180 | struct cifs_fid *fid, struct cifs_open_info_data *buf) |
| 181 | { |
| 182 | int rc = -ENOENT; |
| 183 | int create_options = CREATE_NOT_DIR; |
| 184 | int desired_access; |
| 185 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb: inode->i_sb); |
| 186 | struct cifs_tcon *tcon = tlink_tcon(tlink); |
| 187 | const char *full_path; |
| 188 | void *page = alloc_dentry_path(); |
| 189 | struct inode *newinode = NULL; |
| 190 | int disposition; |
| 191 | struct TCP_Server_Info *server = tcon->ses->server; |
| 192 | struct cifs_open_parms oparms; |
| 193 | struct cached_fid *parent_cfid = NULL; |
| 194 | int rdwr_for_fscache = 0; |
| 195 | __le32 lease_flags = 0; |
| 196 | |
| 197 | *oplock = 0; |
| 198 | if (tcon->ses->server->oplocks) |
| 199 | *oplock = REQ_OPLOCK; |
| 200 | |
| 201 | full_path = build_path_from_dentry(direntry, page); |
| 202 | if (IS_ERR(ptr: full_path)) { |
| 203 | rc = PTR_ERR(ptr: full_path); |
| 204 | goto out; |
| 205 | } |
| 206 | |
| 207 | /* If we're caching, we need to be able to fill in around partial writes. */ |
| 208 | if (cifs_fscache_enabled(inode) && (oflags & O_ACCMODE) == O_WRONLY) |
| 209 | rdwr_for_fscache = 1; |
| 210 | |
| 211 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 212 | if (tcon->unix_ext && cap_unix(ses: tcon->ses) && !tcon->broken_posix_open && |
| 213 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
| 214 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
| 215 | rc = cifs_posix_open(full_path, inode: &newinode, sb: inode->i_sb, mode, |
| 216 | f_flags: oflags, oplock, netfid: &fid->netfid, xid); |
| 217 | switch (rc) { |
| 218 | case 0: |
| 219 | if (newinode == NULL) { |
| 220 | /* query inode info */ |
| 221 | goto cifs_create_get_file_info; |
| 222 | } |
| 223 | |
| 224 | if (S_ISDIR(newinode->i_mode)) { |
| 225 | CIFSSMBClose(xid, tcon, smb_file_id: fid->netfid); |
| 226 | iput(newinode); |
| 227 | rc = -EISDIR; |
| 228 | goto out; |
| 229 | } |
| 230 | |
| 231 | if (!S_ISREG(newinode->i_mode)) { |
| 232 | /* |
| 233 | * The server may allow us to open things like |
| 234 | * FIFOs, but the client isn't set up to deal |
| 235 | * with that. If it's not a regular file, just |
| 236 | * close it and proceed as if it were a normal |
| 237 | * lookup. |
| 238 | */ |
| 239 | CIFSSMBClose(xid, tcon, smb_file_id: fid->netfid); |
| 240 | goto cifs_create_get_file_info; |
| 241 | } |
| 242 | /* success, no need to query */ |
| 243 | goto cifs_create_set_dentry; |
| 244 | |
| 245 | case -ENOENT: |
| 246 | goto cifs_create_get_file_info; |
| 247 | |
| 248 | case -EIO: |
| 249 | case -EINVAL: |
| 250 | /* |
| 251 | * EIO could indicate that (posix open) operation is not |
| 252 | * supported, despite what server claimed in capability |
| 253 | * negotiation. |
| 254 | * |
| 255 | * POSIX open in samba versions 3.3.1 and earlier could |
| 256 | * incorrectly fail with invalid parameter. |
| 257 | */ |
| 258 | tcon->broken_posix_open = true; |
| 259 | break; |
| 260 | |
| 261 | case -EREMOTE: |
| 262 | case -EOPNOTSUPP: |
| 263 | /* |
| 264 | * EREMOTE indicates DFS junction, which is not handled |
| 265 | * in posix open. If either that or op not supported |
| 266 | * returned, follow the normal lookup. |
| 267 | */ |
| 268 | break; |
| 269 | |
| 270 | default: |
| 271 | goto out; |
| 272 | } |
| 273 | /* |
| 274 | * fallthrough to retry, using older open call, this is case |
| 275 | * where server does not support this SMB level, and falsely |
| 276 | * claims capability (also get here for DFS case which should be |
| 277 | * rare for path not covered on files) |
| 278 | */ |
| 279 | } |
| 280 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 281 | |
| 282 | desired_access = 0; |
| 283 | if (OPEN_FMODE(oflags) & FMODE_READ) |
| 284 | desired_access |= GENERIC_READ; /* is this too little? */ |
| 285 | if (OPEN_FMODE(oflags) & FMODE_WRITE) |
| 286 | desired_access |= GENERIC_WRITE; |
| 287 | if (rdwr_for_fscache == 1) |
| 288 | desired_access |= GENERIC_READ; |
| 289 | |
| 290 | disposition = FILE_OVERWRITE_IF; |
| 291 | if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
| 292 | disposition = FILE_CREATE; |
| 293 | else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) |
| 294 | disposition = FILE_OVERWRITE_IF; |
| 295 | else if ((oflags & O_CREAT) == O_CREAT) |
| 296 | disposition = FILE_OPEN_IF; |
| 297 | else |
| 298 | cifs_dbg(FYI, "Create flag not set in create function\n" ); |
| 299 | |
| 300 | /* |
| 301 | * BB add processing to set equivalent of mode - e.g. via CreateX with |
| 302 | * ACLs |
| 303 | */ |
| 304 | |
| 305 | if (!server->ops->open) { |
| 306 | rc = -ENOSYS; |
| 307 | goto out; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * if we're not using unix extensions, see if we need to set |
| 312 | * ATTR_READONLY on the create call |
| 313 | */ |
| 314 | if (!tcon->unix_ext && (mode & S_IWUGO) == 0) |
| 315 | create_options |= CREATE_OPTION_READONLY; |
| 316 | |
| 317 | |
| 318 | retry_open: |
| 319 | if (tcon->cfids && direntry->d_parent && server->dialect >= SMB30_PROT_ID) { |
| 320 | parent_cfid = NULL; |
| 321 | spin_lock(lock: &tcon->cfids->cfid_list_lock); |
| 322 | list_for_each_entry(parent_cfid, &tcon->cfids->entries, entry) { |
| 323 | if (parent_cfid->dentry == direntry->d_parent) { |
| 324 | cifs_dbg(FYI, "found a parent cached file handle\n" ); |
| 325 | if (is_valid_cached_dir(cfid: parent_cfid)) { |
| 326 | lease_flags |
| 327 | |= SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE; |
| 328 | memcpy(fid->parent_lease_key, |
| 329 | parent_cfid->fid.lease_key, |
| 330 | SMB2_LEASE_KEY_SIZE); |
| 331 | parent_cfid->dirents.is_valid = false; |
| 332 | parent_cfid->dirents.is_failed = true; |
| 333 | } |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | spin_unlock(lock: &tcon->cfids->cfid_list_lock); |
| 338 | } |
| 339 | |
| 340 | oparms = (struct cifs_open_parms) { |
| 341 | .tcon = tcon, |
| 342 | .cifs_sb = cifs_sb, |
| 343 | .desired_access = desired_access, |
| 344 | .create_options = cifs_create_options(cifs_sb, options: create_options), |
| 345 | .disposition = disposition, |
| 346 | .path = full_path, |
| 347 | .fid = fid, |
| 348 | .lease_flags = lease_flags, |
| 349 | .mode = mode, |
| 350 | }; |
| 351 | rc = server->ops->open(xid, &oparms, oplock, buf); |
| 352 | if (rc) { |
| 353 | cifs_dbg(FYI, "cifs_create returned 0x%x\n" , rc); |
| 354 | if (rc == -EACCES && rdwr_for_fscache == 1) { |
| 355 | desired_access &= ~GENERIC_READ; |
| 356 | rdwr_for_fscache = 2; |
| 357 | goto retry_open; |
| 358 | } |
| 359 | goto out; |
| 360 | } |
| 361 | if (rdwr_for_fscache == 2) |
| 362 | cifs_invalidate_cache(inode, FSCACHE_INVAL_DIO_WRITE); |
| 363 | |
| 364 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 365 | /* |
| 366 | * If Open reported that we actually created a file then we now have to |
| 367 | * set the mode if possible. |
| 368 | */ |
| 369 | if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) { |
| 370 | struct cifs_unix_set_info_args args = { |
| 371 | .mode = mode, |
| 372 | .ctime = NO_CHANGE_64, |
| 373 | .atime = NO_CHANGE_64, |
| 374 | .mtime = NO_CHANGE_64, |
| 375 | .device = 0, |
| 376 | }; |
| 377 | |
| 378 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
| 379 | args.uid = current_fsuid(); |
| 380 | if (inode->i_mode & S_ISGID) |
| 381 | args.gid = inode->i_gid; |
| 382 | else |
| 383 | args.gid = current_fsgid(); |
| 384 | } else { |
| 385 | args.uid = INVALID_UID; /* no change */ |
| 386 | args.gid = INVALID_GID; /* no change */ |
| 387 | } |
| 388 | CIFSSMBUnixSetFileInfo(xid, tcon, args: &args, fid: fid->netfid, |
| 389 | current->tgid); |
| 390 | } else { |
| 391 | /* |
| 392 | * BB implement mode setting via Windows security |
| 393 | * descriptors e.g. |
| 394 | */ |
| 395 | /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/ |
| 396 | |
| 397 | /* Could set r/o dos attribute if mode & 0222 == 0 */ |
| 398 | } |
| 399 | |
| 400 | cifs_create_get_file_info: |
| 401 | /* server might mask mode so we have to query for it */ |
| 402 | if (tcon->unix_ext) |
| 403 | rc = cifs_get_inode_info_unix(pinode: &newinode, search_path: full_path, sb: inode->i_sb, |
| 404 | xid); |
| 405 | else { |
| 406 | #else |
| 407 | { |
| 408 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 409 | /* TODO: Add support for calling POSIX query info here, but passing in fid */ |
| 410 | rc = cifs_get_inode_info(inode: &newinode, full_path, data: buf, sb: inode->i_sb, xid, fid); |
| 411 | if (newinode) { |
| 412 | if (server->ops->set_lease_key) |
| 413 | server->ops->set_lease_key(newinode, fid); |
| 414 | if ((*oplock & CIFS_CREATE_ACTION) && S_ISREG(newinode->i_mode)) { |
| 415 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) |
| 416 | newinode->i_mode = mode; |
| 417 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
| 418 | newinode->i_uid = current_fsuid(); |
| 419 | if (inode->i_mode & S_ISGID) |
| 420 | newinode->i_gid = inode->i_gid; |
| 421 | else |
| 422 | newinode->i_gid = current_fsgid(); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY |
| 429 | cifs_create_set_dentry: |
| 430 | #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */ |
| 431 | if (rc != 0) { |
| 432 | cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n" , |
| 433 | rc); |
| 434 | goto out_err; |
| 435 | } |
| 436 | |
| 437 | if (newinode) |
| 438 | if (S_ISDIR(newinode->i_mode)) { |
| 439 | rc = -EISDIR; |
| 440 | goto out_err; |
| 441 | } |
| 442 | |
| 443 | d_drop(dentry: direntry); |
| 444 | d_add(direntry, newinode); |
| 445 | |
| 446 | out: |
| 447 | free_dentry_path(page); |
| 448 | return rc; |
| 449 | |
| 450 | out_err: |
| 451 | if (server->ops->close) |
| 452 | server->ops->close(xid, tcon, fid); |
| 453 | if (newinode) |
| 454 | iput(newinode); |
| 455 | goto out; |
| 456 | } |
| 457 | |
| 458 | int |
| 459 | cifs_atomic_open(struct inode *inode, struct dentry *direntry, |
| 460 | struct file *file, unsigned int oflags, umode_t mode) |
| 461 | { |
| 462 | int rc; |
| 463 | unsigned int xid; |
| 464 | struct tcon_link *tlink; |
| 465 | struct cifs_tcon *tcon; |
| 466 | struct TCP_Server_Info *server; |
| 467 | struct cifs_fid fid = {}; |
| 468 | struct cifs_pending_open open; |
| 469 | __u32 oplock; |
| 470 | struct cifsFileInfo *file_info; |
| 471 | struct cifs_open_info_data buf = {}; |
| 472 | |
| 473 | if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb)))) |
| 474 | return smb_EIO(trace: smb_eio_trace_forced_shutdown); |
| 475 | |
| 476 | /* |
| 477 | * Posix open is only called (at lookup time) for file create now. For |
| 478 | * opens (rather than creates), because we do not know if it is a file |
| 479 | * or directory yet, and current Samba no longer allows us to do posix |
| 480 | * open on dirs, we could end up wasting an open call on what turns out |
| 481 | * to be a dir. For file opens, we wait to call posix open till |
| 482 | * cifs_open. It could be added to atomic_open in the future but the |
| 483 | * performance tradeoff of the extra network request when EISDIR or |
| 484 | * EACCES is returned would have to be weighed against the 50% reduction |
| 485 | * in network traffic in the other paths. |
| 486 | */ |
| 487 | if (!(oflags & O_CREAT)) { |
| 488 | /* |
| 489 | * Check for hashed negative dentry. We have already revalidated |
| 490 | * the dentry and it is fine. No need to perform another lookup. |
| 491 | */ |
| 492 | if (!d_in_lookup(dentry: direntry)) |
| 493 | return -ENOENT; |
| 494 | |
| 495 | return finish_no_open(file, dentry: cifs_lookup(inode, direntry, 0)); |
| 496 | } |
| 497 | |
| 498 | xid = get_xid(); |
| 499 | |
| 500 | cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n" , |
| 501 | inode, direntry, direntry); |
| 502 | |
| 503 | tlink = cifs_sb_tlink(cifs_sb: CIFS_SB(sb: inode->i_sb)); |
| 504 | if (IS_ERR(ptr: tlink)) { |
| 505 | rc = PTR_ERR(ptr: tlink); |
| 506 | goto out_free_xid; |
| 507 | } |
| 508 | |
| 509 | tcon = tlink_tcon(tlink); |
| 510 | |
| 511 | rc = check_name(direntry, tcon); |
| 512 | if (rc) |
| 513 | goto out; |
| 514 | |
| 515 | server = tcon->ses->server; |
| 516 | |
| 517 | if (server->ops->new_lease_key) |
| 518 | server->ops->new_lease_key(&fid); |
| 519 | |
| 520 | cifs_add_pending_open(fid: &fid, tlink, open: &open); |
| 521 | |
| 522 | rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode, |
| 523 | oplock: &oplock, fid: &fid, buf: &buf); |
| 524 | if (rc) { |
| 525 | cifs_del_pending_open(open: &open); |
| 526 | goto out; |
| 527 | } |
| 528 | |
| 529 | if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) |
| 530 | file->f_mode |= FMODE_CREATED; |
| 531 | |
| 532 | rc = finish_open(file, dentry: direntry, open: generic_file_open); |
| 533 | if (rc) { |
| 534 | if (server->ops->close) |
| 535 | server->ops->close(xid, tcon, &fid); |
| 536 | cifs_del_pending_open(open: &open); |
| 537 | goto out; |
| 538 | } |
| 539 | |
| 540 | if (file->f_flags & O_DIRECT && |
| 541 | CIFS_SB(sb: inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) { |
| 542 | if (CIFS_SB(sb: inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
| 543 | file->f_op = &cifs_file_direct_nobrl_ops; |
| 544 | else |
| 545 | file->f_op = &cifs_file_direct_ops; |
| 546 | } |
| 547 | |
| 548 | file_info = cifs_new_fileinfo(fid: &fid, file, tlink, oplock, symlink_target: buf.symlink_target); |
| 549 | if (file_info == NULL) { |
| 550 | if (server->ops->close) |
| 551 | server->ops->close(xid, tcon, &fid); |
| 552 | cifs_del_pending_open(open: &open); |
| 553 | rc = -ENOMEM; |
| 554 | goto out; |
| 555 | } |
| 556 | |
| 557 | fscache_use_cookie(cookie: cifs_inode_cookie(inode: file_inode(f: file)), |
| 558 | will_modify: file->f_mode & FMODE_WRITE); |
| 559 | |
| 560 | out: |
| 561 | cifs_put_tlink(tlink); |
| 562 | out_free_xid: |
| 563 | free_xid(xid); |
| 564 | cifs_free_open_info(data: &buf); |
| 565 | return rc; |
| 566 | } |
| 567 | |
| 568 | int cifs_create(struct mnt_idmap *idmap, struct inode *inode, |
| 569 | struct dentry *direntry, umode_t mode, bool excl) |
| 570 | { |
| 571 | int rc; |
| 572 | unsigned int xid = get_xid(); |
| 573 | /* |
| 574 | * BB below access is probably too much for mknod to request |
| 575 | * but we have to do query and setpathinfo so requesting |
| 576 | * less could fail (unless we want to request getatr and setatr |
| 577 | * permissions (only). At least for POSIX we do not have to |
| 578 | * request so much. |
| 579 | */ |
| 580 | unsigned oflags = O_EXCL | O_CREAT | O_RDWR; |
| 581 | struct tcon_link *tlink; |
| 582 | struct cifs_tcon *tcon; |
| 583 | struct TCP_Server_Info *server; |
| 584 | struct cifs_fid fid; |
| 585 | __u32 oplock; |
| 586 | struct cifs_open_info_data buf = {}; |
| 587 | |
| 588 | cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n" , |
| 589 | inode, direntry, direntry); |
| 590 | |
| 591 | if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb)))) { |
| 592 | rc = smb_EIO(trace: smb_eio_trace_forced_shutdown); |
| 593 | goto out_free_xid; |
| 594 | } |
| 595 | |
| 596 | tlink = cifs_sb_tlink(cifs_sb: CIFS_SB(sb: inode->i_sb)); |
| 597 | rc = PTR_ERR(ptr: tlink); |
| 598 | if (IS_ERR(ptr: tlink)) |
| 599 | goto out_free_xid; |
| 600 | |
| 601 | tcon = tlink_tcon(tlink); |
| 602 | server = tcon->ses->server; |
| 603 | |
| 604 | if (server->ops->new_lease_key) |
| 605 | server->ops->new_lease_key(&fid); |
| 606 | |
| 607 | rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode, oplock: &oplock, fid: &fid, buf: &buf); |
| 608 | if (!rc && server->ops->close) |
| 609 | server->ops->close(xid, tcon, &fid); |
| 610 | |
| 611 | cifs_free_open_info(data: &buf); |
| 612 | cifs_put_tlink(tlink); |
| 613 | out_free_xid: |
| 614 | free_xid(xid); |
| 615 | return rc; |
| 616 | } |
| 617 | |
| 618 | int cifs_mknod(struct mnt_idmap *idmap, struct inode *inode, |
| 619 | struct dentry *direntry, umode_t mode, dev_t device_number) |
| 620 | { |
| 621 | int rc = -EPERM; |
| 622 | unsigned int xid; |
| 623 | struct cifs_sb_info *cifs_sb; |
| 624 | struct tcon_link *tlink; |
| 625 | struct cifs_tcon *tcon; |
| 626 | const char *full_path; |
| 627 | void *page; |
| 628 | |
| 629 | if (!old_valid_dev(dev: device_number)) |
| 630 | return -EINVAL; |
| 631 | |
| 632 | cifs_sb = CIFS_SB(sb: inode->i_sb); |
| 633 | if (unlikely(cifs_forced_shutdown(cifs_sb))) |
| 634 | return smb_EIO(trace: smb_eio_trace_forced_shutdown); |
| 635 | |
| 636 | tlink = cifs_sb_tlink(cifs_sb); |
| 637 | if (IS_ERR(ptr: tlink)) |
| 638 | return PTR_ERR(ptr: tlink); |
| 639 | |
| 640 | page = alloc_dentry_path(); |
| 641 | tcon = tlink_tcon(tlink); |
| 642 | xid = get_xid(); |
| 643 | |
| 644 | full_path = build_path_from_dentry(direntry, page); |
| 645 | if (IS_ERR(ptr: full_path)) { |
| 646 | rc = PTR_ERR(ptr: full_path); |
| 647 | goto mknod_out; |
| 648 | } |
| 649 | |
| 650 | trace_smb3_mknod_enter(xid, tid: tcon->tid, sesid: tcon->ses->Suid, full_path); |
| 651 | |
| 652 | rc = tcon->ses->server->ops->make_node(xid, inode, direntry, tcon, |
| 653 | full_path, mode, |
| 654 | device_number); |
| 655 | |
| 656 | mknod_out: |
| 657 | if (rc) |
| 658 | trace_smb3_mknod_err(xid, tid: tcon->tid, sesid: tcon->ses->Suid, rc); |
| 659 | else |
| 660 | trace_smb3_mknod_done(xid, tid: tcon->tid, sesid: tcon->ses->Suid); |
| 661 | |
| 662 | free_dentry_path(page); |
| 663 | free_xid(xid); |
| 664 | cifs_put_tlink(tlink); |
| 665 | return rc; |
| 666 | } |
| 667 | |
| 668 | struct dentry * |
| 669 | cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, |
| 670 | unsigned int flags) |
| 671 | { |
| 672 | unsigned int xid; |
| 673 | int rc = 0; /* to get around spurious gcc warning, set to zero here */ |
| 674 | struct cifs_sb_info *cifs_sb; |
| 675 | struct tcon_link *tlink; |
| 676 | struct cifs_tcon *pTcon; |
| 677 | struct inode *newInode = NULL; |
| 678 | const char *full_path; |
| 679 | void *page; |
| 680 | int retry_count = 0; |
| 681 | struct dentry *de; |
| 682 | |
| 683 | xid = get_xid(); |
| 684 | |
| 685 | cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n" , |
| 686 | parent_dir_inode, direntry, direntry); |
| 687 | |
| 688 | /* check whether path exists */ |
| 689 | |
| 690 | cifs_sb = CIFS_SB(sb: parent_dir_inode->i_sb); |
| 691 | tlink = cifs_sb_tlink(cifs_sb); |
| 692 | if (IS_ERR(ptr: tlink)) { |
| 693 | de = ERR_CAST(ptr: tlink); |
| 694 | goto free_xid; |
| 695 | } |
| 696 | pTcon = tlink_tcon(tlink); |
| 697 | |
| 698 | rc = check_name(direntry, tcon: pTcon); |
| 699 | if (unlikely(rc)) { |
| 700 | de = ERR_PTR(error: rc); |
| 701 | goto put_tlink; |
| 702 | } |
| 703 | |
| 704 | /* can not grab the rename sem here since it would |
| 705 | deadlock in the cases (beginning of sys_rename itself) |
| 706 | in which we already have the sb rename sem */ |
| 707 | page = alloc_dentry_path(); |
| 708 | full_path = build_path_from_dentry(direntry, page); |
| 709 | if (IS_ERR(ptr: full_path)) { |
| 710 | de = ERR_CAST(ptr: full_path); |
| 711 | goto free_dentry_path; |
| 712 | } |
| 713 | |
| 714 | if (d_really_is_positive(dentry: direntry)) { |
| 715 | cifs_dbg(FYI, "non-NULL inode in lookup\n" ); |
| 716 | } else { |
| 717 | struct cached_fid *cfid = NULL; |
| 718 | |
| 719 | cifs_dbg(FYI, "NULL inode in lookup\n" ); |
| 720 | |
| 721 | /* |
| 722 | * We can only rely on negative dentries having the same |
| 723 | * spelling as the cached dirent if case insensitivity is |
| 724 | * forced on mount. |
| 725 | * |
| 726 | * XXX: if servers correctly announce Case Sensitivity Search |
| 727 | * on GetInfo of FileFSAttributeInformation, then we can take |
| 728 | * correct action even if case insensitive is not forced on |
| 729 | * mount. |
| 730 | */ |
| 731 | if (pTcon->nocase && !open_cached_dir_by_dentry(tcon: pTcon, dentry: direntry->d_parent, cfid: &cfid)) { |
| 732 | /* |
| 733 | * dentry is negative and parent is fully cached: |
| 734 | * we can assume file does not exist |
| 735 | */ |
| 736 | if (cfid->dirents.is_valid) { |
| 737 | close_cached_dir(cfid); |
| 738 | goto out; |
| 739 | } |
| 740 | close_cached_dir(cfid); |
| 741 | } |
| 742 | } |
| 743 | cifs_dbg(FYI, "Full path: %s inode = 0x%p\n" , |
| 744 | full_path, d_inode(direntry)); |
| 745 | |
| 746 | again: |
| 747 | if (pTcon->posix_extensions) { |
| 748 | rc = smb311_posix_get_inode_info(inode: &newInode, full_path, NULL, |
| 749 | sb: parent_dir_inode->i_sb, xid); |
| 750 | } else if (pTcon->unix_ext) { |
| 751 | rc = cifs_get_inode_info_unix(pinode: &newInode, search_path: full_path, |
| 752 | sb: parent_dir_inode->i_sb, xid); |
| 753 | } else { |
| 754 | rc = cifs_get_inode_info(inode: &newInode, full_path, NULL, |
| 755 | sb: parent_dir_inode->i_sb, xid, NULL); |
| 756 | } |
| 757 | |
| 758 | if (rc == 0) { |
| 759 | /* since paths are not looked up by component - the parent |
| 760 | directories are presumed to be good here */ |
| 761 | renew_parental_timestamps(direntry); |
| 762 | } else if (rc == -EAGAIN && retry_count++ < 10) { |
| 763 | goto again; |
| 764 | } else if (rc == -ENOENT) { |
| 765 | cifs_set_time(dentry: direntry, time: jiffies); |
| 766 | newInode = NULL; |
| 767 | } else { |
| 768 | if (rc != -EACCES) { |
| 769 | cifs_dbg(FYI, "Unexpected lookup error %d\n" , rc); |
| 770 | /* We special case check for Access Denied - since that |
| 771 | is a common return code */ |
| 772 | } |
| 773 | newInode = ERR_PTR(error: rc); |
| 774 | } |
| 775 | |
| 776 | out: |
| 777 | de = d_splice_alias(newInode, direntry); |
| 778 | free_dentry_path: |
| 779 | free_dentry_path(page); |
| 780 | put_tlink: |
| 781 | cifs_put_tlink(tlink); |
| 782 | free_xid: |
| 783 | free_xid(xid); |
| 784 | return de; |
| 785 | } |
| 786 | |
| 787 | static int |
| 788 | cifs_d_revalidate(struct inode *dir, const struct qstr *name, |
| 789 | struct dentry *direntry, unsigned int flags) |
| 790 | { |
| 791 | if (flags & LOOKUP_RCU) |
| 792 | return -ECHILD; |
| 793 | |
| 794 | if (d_really_is_positive(dentry: direntry)) { |
| 795 | int rc; |
| 796 | struct inode *inode = d_inode(dentry: direntry); |
| 797 | |
| 798 | if ((flags & LOOKUP_REVAL) && !CIFS_CACHE_READ(CIFS_I(inode))) |
| 799 | CIFS_I(inode)->time = 0; /* force reval */ |
| 800 | |
| 801 | rc = cifs_revalidate_dentry(direntry); |
| 802 | if (rc) { |
| 803 | cifs_dbg(FYI, "cifs_revalidate_dentry failed with rc=%d" , rc); |
| 804 | switch (rc) { |
| 805 | case -ENOENT: |
| 806 | case -ESTALE: |
| 807 | /* |
| 808 | * Those errors mean the dentry is invalid |
| 809 | * (file was deleted or recreated) |
| 810 | */ |
| 811 | return 0; |
| 812 | default: |
| 813 | /* |
| 814 | * Otherwise some unexpected error happened |
| 815 | * report it as-is to VFS layer |
| 816 | */ |
| 817 | return rc; |
| 818 | } |
| 819 | } |
| 820 | else { |
| 821 | /* |
| 822 | * If the inode wasn't known to be a dfs entry when |
| 823 | * the dentry was instantiated, such as when created |
| 824 | * via ->readdir(), it needs to be set now since the |
| 825 | * attributes will have been updated by |
| 826 | * cifs_revalidate_dentry(). |
| 827 | */ |
| 828 | if (IS_AUTOMOUNT(inode) && |
| 829 | !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) { |
| 830 | spin_lock(lock: &direntry->d_lock); |
| 831 | direntry->d_flags |= DCACHE_NEED_AUTOMOUNT; |
| 832 | spin_unlock(lock: &direntry->d_lock); |
| 833 | } |
| 834 | |
| 835 | return 1; |
| 836 | } |
| 837 | } else { |
| 838 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb: dir->i_sb); |
| 839 | struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); |
| 840 | struct cached_fid *cfid; |
| 841 | |
| 842 | if (!open_cached_dir_by_dentry(tcon, dentry: direntry->d_parent, cfid: &cfid)) { |
| 843 | /* |
| 844 | * dentry is negative and parent is fully cached: |
| 845 | * we can assume file does not exist |
| 846 | */ |
| 847 | if (cfid->dirents.is_valid) { |
| 848 | close_cached_dir(cfid); |
| 849 | return 1; |
| 850 | } |
| 851 | close_cached_dir(cfid); |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * This may be nfsd (or something), anyway, we can't see the |
| 857 | * intent of this. So, since this can be for creation, drop it. |
| 858 | */ |
| 859 | if (!flags) |
| 860 | return 0; |
| 861 | |
| 862 | /* |
| 863 | * Drop the negative dentry, in order to make sure to use the |
| 864 | * case sensitive name which is specified by user if this is |
| 865 | * for creation. |
| 866 | */ |
| 867 | if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) |
| 868 | return 0; |
| 869 | |
| 870 | if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled) |
| 871 | return 0; |
| 872 | |
| 873 | return 1; |
| 874 | } |
| 875 | |
| 876 | /* static int cifs_d_delete(struct dentry *direntry) |
| 877 | { |
| 878 | int rc = 0; |
| 879 | |
| 880 | cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry); |
| 881 | |
| 882 | return rc; |
| 883 | } */ |
| 884 | |
| 885 | const struct dentry_operations cifs_dentry_ops = { |
| 886 | .d_revalidate = cifs_d_revalidate, |
| 887 | .d_automount = cifs_d_automount, |
| 888 | /* d_delete: cifs_d_delete, */ /* not needed except for debugging */ |
| 889 | }; |
| 890 | |
| 891 | static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q) |
| 892 | { |
| 893 | struct nls_table *codepage = CIFS_SB(sb: dentry->d_sb)->local_nls; |
| 894 | unsigned long hash; |
| 895 | wchar_t c; |
| 896 | int i, charlen; |
| 897 | |
| 898 | hash = init_name_hash(dentry); |
| 899 | for (i = 0; i < q->len; i += charlen) { |
| 900 | charlen = codepage->char2uni(&q->name[i], q->len - i, &c); |
| 901 | /* error out if we can't convert the character */ |
| 902 | if (unlikely(charlen < 0)) |
| 903 | return charlen; |
| 904 | hash = partial_name_hash(c: cifs_toupper(in: c), prevhash: hash); |
| 905 | } |
| 906 | q->hash = end_name_hash(hash); |
| 907 | |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | static int cifs_ci_compare(const struct dentry *dentry, |
| 912 | unsigned int len, const char *str, const struct qstr *name) |
| 913 | { |
| 914 | struct nls_table *codepage = CIFS_SB(sb: dentry->d_sb)->local_nls; |
| 915 | wchar_t c1, c2; |
| 916 | int i, l1, l2; |
| 917 | |
| 918 | /* |
| 919 | * We make the assumption here that uppercase characters in the local |
| 920 | * codepage are always the same length as their lowercase counterparts. |
| 921 | * |
| 922 | * If that's ever not the case, then this will fail to match it. |
| 923 | */ |
| 924 | if (name->len != len) |
| 925 | return 1; |
| 926 | |
| 927 | for (i = 0; i < len; i += l1) { |
| 928 | /* Convert characters in both strings to UTF-16. */ |
| 929 | l1 = codepage->char2uni(&str[i], len - i, &c1); |
| 930 | l2 = codepage->char2uni(&name->name[i], name->len - i, &c2); |
| 931 | |
| 932 | /* |
| 933 | * If we can't convert either character, just declare it to |
| 934 | * be 1 byte long and compare the original byte. |
| 935 | */ |
| 936 | if (unlikely(l1 < 0 && l2 < 0)) { |
| 937 | if (str[i] != name->name[i]) |
| 938 | return 1; |
| 939 | l1 = 1; |
| 940 | continue; |
| 941 | } |
| 942 | |
| 943 | /* |
| 944 | * Here, we again ass|u|me that upper/lowercase versions of |
| 945 | * a character are the same length in the local NLS. |
| 946 | */ |
| 947 | if (l1 != l2) |
| 948 | return 1; |
| 949 | |
| 950 | /* Now compare uppercase versions of these characters */ |
| 951 | if (cifs_toupper(in: c1) != cifs_toupper(in: c2)) |
| 952 | return 1; |
| 953 | } |
| 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | const struct dentry_operations cifs_ci_dentry_ops = { |
| 959 | .d_revalidate = cifs_d_revalidate, |
| 960 | .d_hash = cifs_ci_hash, |
| 961 | .d_compare = cifs_ci_compare, |
| 962 | .d_automount = cifs_d_automount, |
| 963 | }; |
| 964 | |