dir.c 23 KB

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