dir.c 22 KB

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