dir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /*
  2. * fs/cifs/dir.c
  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. * This library is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as published
  11. * by the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  17. * the GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/fs.h>
  24. #include <linux/stat.h>
  25. #include <linux/slab.h>
  26. #include <linux/namei.h>
  27. #include <linux/mount.h>
  28. #include <linux/file.h>
  29. #include "cifsfs.h"
  30. #include "cifspdu.h"
  31. #include "cifsglob.h"
  32. #include "cifsproto.h"
  33. #include "cifs_debug.h"
  34. #include "cifs_fs_sb.h"
  35. #include "cifs_unicode.h"
  36. static void
  37. renew_parental_timestamps(struct dentry *direntry)
  38. {
  39. /* BB check if there is a way to get the kernel to do this or if we
  40. really need this */
  41. do {
  42. cifs_set_time(direntry, jiffies);
  43. direntry = direntry->d_parent;
  44. } while (!IS_ROOT(direntry));
  45. }
  46. char *
  47. cifs_build_path_to_root(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
  48. struct cifs_tcon *tcon, int add_treename)
  49. {
  50. int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
  51. int dfsplen;
  52. char *full_path = NULL;
  53. /* if no prefix path, simply set path to the root of share to "" */
  54. if (pplen == 0) {
  55. full_path = kzalloc(1, GFP_KERNEL);
  56. return full_path;
  57. }
  58. if (add_treename)
  59. dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
  60. else
  61. dfsplen = 0;
  62. full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
  63. if (full_path == NULL)
  64. return full_path;
  65. if (dfsplen)
  66. memcpy(full_path, tcon->treeName, dfsplen);
  67. full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb);
  68. memcpy(full_path + dfsplen + 1, vol->prepath, pplen);
  69. convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
  70. return full_path;
  71. }
  72. /* Note: caller must free return buffer */
  73. char *
  74. build_path_from_dentry(struct dentry *direntry)
  75. {
  76. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  77. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  78. bool prefix = tcon->Flags & SMB_SHARE_IS_IN_DFS;
  79. return build_path_from_dentry_optional_prefix(direntry,
  80. prefix);
  81. }
  82. char *
  83. build_path_from_dentry_optional_prefix(struct dentry *direntry, bool prefix)
  84. {
  85. struct dentry *temp;
  86. int namelen;
  87. int dfsplen;
  88. int pplen = 0;
  89. char *full_path;
  90. char dirsep;
  91. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  92. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  93. unsigned seq;
  94. dirsep = CIFS_DIR_SEP(cifs_sb);
  95. if (prefix)
  96. dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
  97. else
  98. dfsplen = 0;
  99. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
  100. pplen = cifs_sb->prepath ? strlen(cifs_sb->prepath) + 1 : 0;
  101. cifs_bp_rename_retry:
  102. namelen = dfsplen + pplen;
  103. seq = read_seqbegin(&rename_lock);
  104. rcu_read_lock();
  105. for (temp = direntry; !IS_ROOT(temp);) {
  106. namelen += (1 + temp->d_name.len);
  107. temp = temp->d_parent;
  108. if (temp == NULL) {
  109. cifs_dbg(VFS, "corrupt dentry\n");
  110. rcu_read_unlock();
  111. return NULL;
  112. }
  113. }
  114. rcu_read_unlock();
  115. full_path = kmalloc(namelen+1, GFP_ATOMIC);
  116. if (full_path == NULL)
  117. return full_path;
  118. full_path[namelen] = 0; /* trailing null */
  119. rcu_read_lock();
  120. for (temp = direntry; !IS_ROOT(temp);) {
  121. spin_lock(&temp->d_lock);
  122. namelen -= 1 + temp->d_name.len;
  123. if (namelen < 0) {
  124. spin_unlock(&temp->d_lock);
  125. break;
  126. } else {
  127. full_path[namelen] = dirsep;
  128. strncpy(full_path + namelen + 1, temp->d_name.name,
  129. temp->d_name.len);
  130. cifs_dbg(FYI, "name: %s\n", full_path + namelen);
  131. }
  132. spin_unlock(&temp->d_lock);
  133. temp = temp->d_parent;
  134. if (temp == NULL) {
  135. cifs_dbg(VFS, "corrupt dentry\n");
  136. rcu_read_unlock();
  137. kfree(full_path);
  138. return NULL;
  139. }
  140. }
  141. rcu_read_unlock();
  142. if (namelen != dfsplen + pplen || read_seqretry(&rename_lock, seq)) {
  143. cifs_dbg(FYI, "did not end path lookup where expected. namelen=%ddfsplen=%d\n",
  144. namelen, dfsplen);
  145. /* presumably this is only possible if racing with a rename
  146. of one of the parent directories (we can not lock the dentries
  147. above us to prevent this, but retrying should be harmless) */
  148. kfree(full_path);
  149. goto cifs_bp_rename_retry;
  150. }
  151. /* DIR_SEP already set for byte 0 / vs \ but not for
  152. subsequent slashes in prepath which currently must
  153. be entered the right way - not sure if there is an alternative
  154. since the '\' is a valid posix character so we can not switch
  155. those safely to '/' if any are found in the middle of the prepath */
  156. /* BB test paths to Windows with '/' in the midst of prepath */
  157. if (pplen) {
  158. int i;
  159. cifs_dbg(FYI, "using cifs_sb prepath <%s>\n", cifs_sb->prepath);
  160. memcpy(full_path+dfsplen+1, cifs_sb->prepath, pplen-1);
  161. full_path[dfsplen] = dirsep;
  162. for (i = 0; i < pplen-1; i++)
  163. if (full_path[dfsplen+1+i] == '/')
  164. full_path[dfsplen+1+i] = CIFS_DIR_SEP(cifs_sb);
  165. }
  166. if (dfsplen) {
  167. strncpy(full_path, tcon->treeName, dfsplen);
  168. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
  169. int i;
  170. for (i = 0; i < dfsplen; i++) {
  171. if (full_path[i] == '\\')
  172. full_path[i] = '/';
  173. }
  174. }
  175. }
  176. return full_path;
  177. }
  178. /*
  179. * Don't allow path components longer than the server max.
  180. * Don't allow the separator character in a path component.
  181. * The VFS will not allow "/", but "\" is allowed by posix.
  182. */
  183. static int
  184. check_name(struct dentry *direntry, struct cifs_tcon *tcon)
  185. {
  186. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  187. int i;
  188. if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength &&
  189. direntry->d_name.len >
  190. le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength)))
  191. return -ENAMETOOLONG;
  192. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
  193. for (i = 0; i < direntry->d_name.len; i++) {
  194. if (direntry->d_name.name[i] == '\\') {
  195. cifs_dbg(FYI, "Invalid file name\n");
  196. return -EINVAL;
  197. }
  198. }
  199. }
  200. return 0;
  201. }
  202. /* Inode operations in similar order to how they appear in Linux file fs.h */
  203. static int
  204. cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid,
  205. struct tcon_link *tlink, unsigned oflags, umode_t mode,
  206. __u32 *oplock, struct cifs_fid *fid)
  207. {
  208. int rc = -ENOENT;
  209. int create_options = CREATE_NOT_DIR;
  210. int desired_access;
  211. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  212. struct cifs_tcon *tcon = tlink_tcon(tlink);
  213. char *full_path = NULL;
  214. FILE_ALL_INFO *buf = NULL;
  215. struct inode *newinode = NULL;
  216. int disposition;
  217. struct TCP_Server_Info *server = tcon->ses->server;
  218. struct cifs_open_parms oparms;
  219. *oplock = 0;
  220. if (tcon->ses->server->oplocks)
  221. *oplock = REQ_OPLOCK;
  222. full_path = build_path_from_dentry(direntry);
  223. if (!full_path)
  224. return -ENOMEM;
  225. if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open &&
  226. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  227. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  228. rc = cifs_posix_open(full_path, &newinode, inode->i_sb, mode,
  229. oflags, oplock, &fid->netfid, xid);
  230. switch (rc) {
  231. case 0:
  232. if (newinode == NULL) {
  233. /* query inode info */
  234. goto cifs_create_get_file_info;
  235. }
  236. if (S_ISDIR(newinode->i_mode)) {
  237. CIFSSMBClose(xid, tcon, fid->netfid);
  238. iput(newinode);
  239. rc = -EISDIR;
  240. goto out;
  241. }
  242. if (!S_ISREG(newinode->i_mode)) {
  243. /*
  244. * The server may allow us to open things like
  245. * FIFOs, but the client isn't set up to deal
  246. * with that. If it's not a regular file, just
  247. * close it and proceed as if it were a normal
  248. * lookup.
  249. */
  250. CIFSSMBClose(xid, tcon, fid->netfid);
  251. goto cifs_create_get_file_info;
  252. }
  253. /* success, no need to query */
  254. goto cifs_create_set_dentry;
  255. case -ENOENT:
  256. goto cifs_create_get_file_info;
  257. case -EIO:
  258. case -EINVAL:
  259. /*
  260. * EIO could indicate that (posix open) operation is not
  261. * supported, despite what server claimed in capability
  262. * negotiation.
  263. *
  264. * POSIX open in samba versions 3.3.1 and earlier could
  265. * incorrectly fail with invalid parameter.
  266. */
  267. tcon->broken_posix_open = true;
  268. break;
  269. case -EREMOTE:
  270. case -EOPNOTSUPP:
  271. /*
  272. * EREMOTE indicates DFS junction, which is not handled
  273. * in posix open. If either that or op not supported
  274. * returned, follow the normal lookup.
  275. */
  276. break;
  277. default:
  278. goto out;
  279. }
  280. /*
  281. * fallthrough to retry, using older open call, this is case
  282. * where server does not support this SMB level, and falsely
  283. * claims capability (also get here for DFS case which should be
  284. * rare for path not covered on files)
  285. */
  286. }
  287. desired_access = 0;
  288. if (OPEN_FMODE(oflags) & FMODE_READ)
  289. desired_access |= GENERIC_READ; /* is this too little? */
  290. if (OPEN_FMODE(oflags) & FMODE_WRITE)
  291. desired_access |= GENERIC_WRITE;
  292. disposition = FILE_OVERWRITE_IF;
  293. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  294. disposition = FILE_CREATE;
  295. else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  296. disposition = FILE_OVERWRITE_IF;
  297. else if ((oflags & O_CREAT) == O_CREAT)
  298. disposition = FILE_OPEN_IF;
  299. else
  300. cifs_dbg(FYI, "Create flag not set in create function\n");
  301. /*
  302. * BB add processing to set equivalent of mode - e.g. via CreateX with
  303. * ACLs
  304. */
  305. if (!server->ops->open) {
  306. rc = -ENOSYS;
  307. goto out;
  308. }
  309. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  310. if (buf == NULL) {
  311. rc = -ENOMEM;
  312. goto out;
  313. }
  314. /*
  315. * if we're not using unix extensions, see if we need to set
  316. * ATTR_READONLY on the create call
  317. */
  318. if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
  319. create_options |= CREATE_OPTION_READONLY;
  320. oparms.tcon = tcon;
  321. oparms.cifs_sb = cifs_sb;
  322. oparms.desired_access = desired_access;
  323. oparms.create_options = cifs_create_options(cifs_sb, create_options);
  324. oparms.disposition = disposition;
  325. oparms.path = full_path;
  326. oparms.fid = fid;
  327. oparms.reconnect = false;
  328. oparms.mode = mode;
  329. rc = server->ops->open(xid, &oparms, oplock, buf);
  330. if (rc) {
  331. cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc);
  332. goto out;
  333. }
  334. /*
  335. * If Open reported that we actually created a file then we now have to
  336. * set the mode if possible.
  337. */
  338. if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) {
  339. struct cifs_unix_set_info_args args = {
  340. .mode = mode,
  341. .ctime = NO_CHANGE_64,
  342. .atime = NO_CHANGE_64,
  343. .mtime = NO_CHANGE_64,
  344. .device = 0,
  345. };
  346. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  347. args.uid = current_fsuid();
  348. if (inode->i_mode & S_ISGID)
  349. args.gid = inode->i_gid;
  350. else
  351. args.gid = current_fsgid();
  352. } else {
  353. args.uid = INVALID_UID; /* no change */
  354. args.gid = INVALID_GID; /* no change */
  355. }
  356. CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid,
  357. current->tgid);
  358. } else {
  359. /*
  360. * BB implement mode setting via Windows security
  361. * descriptors e.g.
  362. */
  363. /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
  364. /* Could set r/o dos attribute if mode & 0222 == 0 */
  365. }
  366. cifs_create_get_file_info:
  367. /* server might mask mode so we have to query for it */
  368. if (tcon->unix_ext)
  369. rc = cifs_get_inode_info_unix(&newinode, full_path, inode->i_sb,
  370. xid);
  371. else {
  372. /* TODO: Add support for calling POSIX query info here, but passing in fid */
  373. rc = cifs_get_inode_info(&newinode, full_path, buf, inode->i_sb,
  374. xid, fid);
  375. if (newinode) {
  376. if (server->ops->set_lease_key)
  377. server->ops->set_lease_key(newinode, fid);
  378. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  379. newinode->i_mode = mode;
  380. if ((*oplock & CIFS_CREATE_ACTION) &&
  381. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) {
  382. newinode->i_uid = current_fsuid();
  383. if (inode->i_mode & S_ISGID)
  384. newinode->i_gid = inode->i_gid;
  385. else
  386. newinode->i_gid = current_fsgid();
  387. }
  388. }
  389. }
  390. cifs_create_set_dentry:
  391. if (rc != 0) {
  392. cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n",
  393. rc);
  394. goto out_err;
  395. }
  396. if (S_ISDIR(newinode->i_mode)) {
  397. rc = -EISDIR;
  398. goto out_err;
  399. }
  400. d_drop(direntry);
  401. d_add(direntry, newinode);
  402. out:
  403. kfree(buf);
  404. kfree(full_path);
  405. return rc;
  406. out_err:
  407. if (server->ops->close)
  408. server->ops->close(xid, tcon, fid);
  409. if (newinode)
  410. iput(newinode);
  411. goto out;
  412. }
  413. int
  414. cifs_atomic_open(struct inode *inode, struct dentry *direntry,
  415. struct file *file, unsigned oflags, umode_t mode)
  416. {
  417. int rc;
  418. unsigned int xid;
  419. struct tcon_link *tlink;
  420. struct cifs_tcon *tcon;
  421. struct TCP_Server_Info *server;
  422. struct cifs_fid fid;
  423. struct cifs_pending_open open;
  424. __u32 oplock;
  425. struct cifsFileInfo *file_info;
  426. /*
  427. * Posix open is only called (at lookup time) for file create now. For
  428. * opens (rather than creates), because we do not know if it is a file
  429. * or directory yet, and current Samba no longer allows us to do posix
  430. * open on dirs, we could end up wasting an open call on what turns out
  431. * to be a dir. For file opens, we wait to call posix open till
  432. * cifs_open. It could be added to atomic_open in the future but the
  433. * performance tradeoff of the extra network request when EISDIR or
  434. * EACCES is returned would have to be weighed against the 50% reduction
  435. * in network traffic in the other paths.
  436. */
  437. if (!(oflags & O_CREAT)) {
  438. struct dentry *res;
  439. /*
  440. * Check for hashed negative dentry. We have already revalidated
  441. * the dentry and it is fine. No need to perform another lookup.
  442. */
  443. if (!d_in_lookup(direntry))
  444. return -ENOENT;
  445. res = cifs_lookup(inode, direntry, 0);
  446. if (IS_ERR(res))
  447. return PTR_ERR(res);
  448. return finish_no_open(file, res);
  449. }
  450. xid = get_xid();
  451. cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
  452. inode, direntry, direntry);
  453. tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
  454. if (IS_ERR(tlink)) {
  455. rc = PTR_ERR(tlink);
  456. goto out_free_xid;
  457. }
  458. tcon = tlink_tcon(tlink);
  459. rc = check_name(direntry, tcon);
  460. if (rc)
  461. goto out;
  462. server = tcon->ses->server;
  463. if (server->ops->new_lease_key)
  464. server->ops->new_lease_key(&fid);
  465. cifs_add_pending_open(&fid, tlink, &open);
  466. rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
  467. &oplock, &fid);
  468. if (rc) {
  469. cifs_del_pending_open(&open);
  470. goto out;
  471. }
  472. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  473. file->f_mode |= FMODE_CREATED;
  474. rc = finish_open(file, direntry, generic_file_open);
  475. if (rc) {
  476. if (server->ops->close)
  477. server->ops->close(xid, tcon, &fid);
  478. cifs_del_pending_open(&open);
  479. goto out;
  480. }
  481. if (file->f_flags & O_DIRECT &&
  482. CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
  483. if (CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  484. file->f_op = &cifs_file_direct_nobrl_ops;
  485. else
  486. file->f_op = &cifs_file_direct_ops;
  487. }
  488. file_info = cifs_new_fileinfo(&fid, file, tlink, oplock);
  489. if (file_info == NULL) {
  490. if (server->ops->close)
  491. server->ops->close(xid, tcon, &fid);
  492. cifs_del_pending_open(&open);
  493. rc = -ENOMEM;
  494. }
  495. out:
  496. cifs_put_tlink(tlink);
  497. out_free_xid:
  498. free_xid(xid);
  499. return rc;
  500. }
  501. int cifs_create(struct inode *inode, struct dentry *direntry, umode_t mode,
  502. bool excl)
  503. {
  504. int rc;
  505. unsigned int xid = get_xid();
  506. /*
  507. * BB below access is probably too much for mknod to request
  508. * but we have to do query and setpathinfo so requesting
  509. * less could fail (unless we want to request getatr and setatr
  510. * permissions (only). At least for POSIX we do not have to
  511. * request so much.
  512. */
  513. unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
  514. struct tcon_link *tlink;
  515. struct cifs_tcon *tcon;
  516. struct TCP_Server_Info *server;
  517. struct cifs_fid fid;
  518. __u32 oplock;
  519. cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
  520. inode, direntry, direntry);
  521. tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
  522. rc = PTR_ERR(tlink);
  523. if (IS_ERR(tlink))
  524. goto out_free_xid;
  525. tcon = tlink_tcon(tlink);
  526. server = tcon->ses->server;
  527. if (server->ops->new_lease_key)
  528. server->ops->new_lease_key(&fid);
  529. rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
  530. &oplock, &fid);
  531. if (!rc && server->ops->close)
  532. server->ops->close(xid, tcon, &fid);
  533. cifs_put_tlink(tlink);
  534. out_free_xid:
  535. free_xid(xid);
  536. return rc;
  537. }
  538. int cifs_mknod(struct inode *inode, struct dentry *direntry, umode_t mode,
  539. dev_t device_number)
  540. {
  541. int rc = -EPERM;
  542. unsigned int xid;
  543. struct cifs_sb_info *cifs_sb;
  544. struct tcon_link *tlink;
  545. struct cifs_tcon *tcon;
  546. char *full_path = NULL;
  547. if (!old_valid_dev(device_number))
  548. return -EINVAL;
  549. cifs_sb = CIFS_SB(inode->i_sb);
  550. tlink = cifs_sb_tlink(cifs_sb);
  551. if (IS_ERR(tlink))
  552. return PTR_ERR(tlink);
  553. tcon = tlink_tcon(tlink);
  554. xid = get_xid();
  555. full_path = build_path_from_dentry(direntry);
  556. if (full_path == NULL) {
  557. rc = -ENOMEM;
  558. goto mknod_out;
  559. }
  560. rc = tcon->ses->server->ops->make_node(xid, inode, direntry, tcon,
  561. full_path, mode,
  562. device_number);
  563. mknod_out:
  564. kfree(full_path);
  565. free_xid(xid);
  566. cifs_put_tlink(tlink);
  567. return rc;
  568. }
  569. struct dentry *
  570. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
  571. unsigned int flags)
  572. {
  573. unsigned int xid;
  574. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  575. struct cifs_sb_info *cifs_sb;
  576. struct tcon_link *tlink;
  577. struct cifs_tcon *pTcon;
  578. struct inode *newInode = NULL;
  579. char *full_path = NULL;
  580. xid = get_xid();
  581. cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
  582. parent_dir_inode, direntry, direntry);
  583. /* check whether path exists */
  584. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  585. tlink = cifs_sb_tlink(cifs_sb);
  586. if (IS_ERR(tlink)) {
  587. free_xid(xid);
  588. return ERR_CAST(tlink);
  589. }
  590. pTcon = tlink_tcon(tlink);
  591. rc = check_name(direntry, pTcon);
  592. if (unlikely(rc)) {
  593. cifs_put_tlink(tlink);
  594. free_xid(xid);
  595. return ERR_PTR(rc);
  596. }
  597. /* can not grab the rename sem here since it would
  598. deadlock in the cases (beginning of sys_rename itself)
  599. in which we already have the sb rename sem */
  600. full_path = build_path_from_dentry(direntry);
  601. if (full_path == NULL) {
  602. cifs_put_tlink(tlink);
  603. free_xid(xid);
  604. return ERR_PTR(-ENOMEM);
  605. }
  606. if (d_really_is_positive(direntry)) {
  607. cifs_dbg(FYI, "non-NULL inode in lookup\n");
  608. } else {
  609. cifs_dbg(FYI, "NULL inode in lookup\n");
  610. }
  611. cifs_dbg(FYI, "Full path: %s inode = 0x%p\n",
  612. full_path, d_inode(direntry));
  613. if (pTcon->posix_extensions)
  614. rc = smb311_posix_get_inode_info(&newInode, full_path, parent_dir_inode->i_sb, xid);
  615. else if (pTcon->unix_ext) {
  616. rc = cifs_get_inode_info_unix(&newInode, full_path,
  617. parent_dir_inode->i_sb, xid);
  618. } else {
  619. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  620. parent_dir_inode->i_sb, xid, NULL);
  621. }
  622. if (rc == 0) {
  623. /* since paths are not looked up by component - the parent
  624. directories are presumed to be good here */
  625. renew_parental_timestamps(direntry);
  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. kfree(full_path);
  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_dfs_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_dfs_d_automount,
  779. };