dir.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. static void
  36. renew_parental_timestamps(struct dentry *direntry)
  37. {
  38. /* BB check if there is a way to get the kernel to do this or if we
  39. really need this */
  40. do {
  41. direntry->d_time = jiffies;
  42. direntry = direntry->d_parent;
  43. } while (!IS_ROOT(direntry));
  44. }
  45. /* Note: caller must free return buffer */
  46. char *
  47. build_path_from_dentry(struct dentry *direntry)
  48. {
  49. struct dentry *temp;
  50. int namelen;
  51. int dfsplen;
  52. char *full_path;
  53. char dirsep;
  54. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  55. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  56. unsigned seq;
  57. dirsep = CIFS_DIR_SEP(cifs_sb);
  58. if (tcon->Flags & SMB_SHARE_IS_IN_DFS)
  59. dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
  60. else
  61. dfsplen = 0;
  62. cifs_bp_rename_retry:
  63. namelen = dfsplen;
  64. seq = read_seqbegin(&rename_lock);
  65. rcu_read_lock();
  66. for (temp = direntry; !IS_ROOT(temp);) {
  67. namelen += (1 + temp->d_name.len);
  68. temp = temp->d_parent;
  69. if (temp == NULL) {
  70. cERROR(1, "corrupt dentry");
  71. rcu_read_unlock();
  72. return NULL;
  73. }
  74. }
  75. rcu_read_unlock();
  76. full_path = kmalloc(namelen+1, GFP_KERNEL);
  77. if (full_path == NULL)
  78. return full_path;
  79. full_path[namelen] = 0; /* trailing null */
  80. rcu_read_lock();
  81. for (temp = direntry; !IS_ROOT(temp);) {
  82. spin_lock(&temp->d_lock);
  83. namelen -= 1 + temp->d_name.len;
  84. if (namelen < 0) {
  85. spin_unlock(&temp->d_lock);
  86. break;
  87. } else {
  88. full_path[namelen] = dirsep;
  89. strncpy(full_path + namelen + 1, temp->d_name.name,
  90. temp->d_name.len);
  91. cFYI(0, "name: %s", full_path + namelen);
  92. }
  93. spin_unlock(&temp->d_lock);
  94. temp = temp->d_parent;
  95. if (temp == NULL) {
  96. cERROR(1, "corrupt dentry");
  97. rcu_read_unlock();
  98. kfree(full_path);
  99. return NULL;
  100. }
  101. }
  102. rcu_read_unlock();
  103. if (namelen != dfsplen || read_seqretry(&rename_lock, seq)) {
  104. cFYI(1, "did not end path lookup where expected. namelen=%d "
  105. "dfsplen=%d", namelen, dfsplen);
  106. /* presumably this is only possible if racing with a rename
  107. of one of the parent directories (we can not lock the dentries
  108. above us to prevent this, but retrying should be harmless) */
  109. kfree(full_path);
  110. goto cifs_bp_rename_retry;
  111. }
  112. /* DIR_SEP already set for byte 0 / vs \ but not for
  113. subsequent slashes in prepath which currently must
  114. be entered the right way - not sure if there is an alternative
  115. since the '\' is a valid posix character so we can not switch
  116. those safely to '/' if any are found in the middle of the prepath */
  117. /* BB test paths to Windows with '/' in the midst of prepath */
  118. if (dfsplen) {
  119. strncpy(full_path, tcon->treeName, dfsplen);
  120. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
  121. int i;
  122. for (i = 0; i < dfsplen; i++) {
  123. if (full_path[i] == '\\')
  124. full_path[i] = '/';
  125. }
  126. }
  127. }
  128. return full_path;
  129. }
  130. /*
  131. * Don't allow the separator character in a path component.
  132. * The VFS will not allow "/", but "\" is allowed by posix.
  133. */
  134. static int
  135. check_name(struct dentry *direntry)
  136. {
  137. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  138. int i;
  139. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
  140. for (i = 0; i < direntry->d_name.len; i++) {
  141. if (direntry->d_name.name[i] == '\\') {
  142. cFYI(1, "Invalid file name");
  143. return -EINVAL;
  144. }
  145. }
  146. }
  147. return 0;
  148. }
  149. /* Inode operations in similar order to how they appear in Linux file fs.h */
  150. static int
  151. cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid,
  152. struct tcon_link *tlink, unsigned oflags, umode_t mode,
  153. __u32 *oplock, __u16 *fileHandle, int *created)
  154. {
  155. int rc = -ENOENT;
  156. int create_options = CREATE_NOT_DIR;
  157. int desiredAccess;
  158. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  159. struct cifs_tcon *tcon = tlink_tcon(tlink);
  160. char *full_path = NULL;
  161. FILE_ALL_INFO *buf = NULL;
  162. struct inode *newinode = NULL;
  163. int disposition;
  164. *oplock = 0;
  165. if (tcon->ses->server->oplocks)
  166. *oplock = REQ_OPLOCK;
  167. full_path = build_path_from_dentry(direntry);
  168. if (full_path == NULL) {
  169. rc = -ENOMEM;
  170. goto out;
  171. }
  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,
  176. inode->i_sb, mode, oflags, oplock, fileHandle, 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_ISREG(newinode->i_mode)) {
  184. /*
  185. * The server may allow us to open things like
  186. * FIFOs, but the client isn't set up to deal
  187. * with that. If it's not a regular file, just
  188. * close it and proceed as if it were a normal
  189. * lookup.
  190. */
  191. CIFSSMBClose(xid, tcon, *fileHandle);
  192. goto cifs_create_get_file_info;
  193. }
  194. /* success, no need to query */
  195. goto cifs_create_set_dentry;
  196. case -ENOENT:
  197. goto cifs_create_get_file_info;
  198. case -EIO:
  199. case -EINVAL:
  200. /*
  201. * EIO could indicate that (posix open) operation is not
  202. * supported, despite what server claimed in capability
  203. * negotiation.
  204. *
  205. * POSIX open in samba versions 3.3.1 and earlier could
  206. * incorrectly fail with invalid parameter.
  207. */
  208. tcon->broken_posix_open = true;
  209. break;
  210. case -EREMOTE:
  211. case -EOPNOTSUPP:
  212. /*
  213. * EREMOTE indicates DFS junction, which is not handled
  214. * in posix open. If either that or op not supported
  215. * returned, follow the normal lookup.
  216. */
  217. break;
  218. default:
  219. goto out;
  220. }
  221. /*
  222. * fallthrough to retry, using older open call, this is case
  223. * where server does not support this SMB level, and falsely
  224. * claims capability (also get here for DFS case which should be
  225. * rare for path not covered on files)
  226. */
  227. }
  228. desiredAccess = 0;
  229. if (OPEN_FMODE(oflags) & FMODE_READ)
  230. desiredAccess |= GENERIC_READ; /* is this too little? */
  231. if (OPEN_FMODE(oflags) & FMODE_WRITE)
  232. desiredAccess |= GENERIC_WRITE;
  233. disposition = FILE_OVERWRITE_IF;
  234. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  235. disposition = FILE_CREATE;
  236. else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  237. disposition = FILE_OVERWRITE_IF;
  238. else if ((oflags & O_CREAT) == O_CREAT)
  239. disposition = FILE_OPEN_IF;
  240. else
  241. cFYI(1, "Create flag not set in create function");
  242. /* BB add processing to set equivalent of mode - e.g. via CreateX with
  243. ACLs */
  244. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  245. if (buf == NULL) {
  246. rc = -ENOMEM;
  247. goto out;
  248. }
  249. /*
  250. * if we're not using unix extensions, see if we need to set
  251. * ATTR_READONLY on the create call
  252. */
  253. if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
  254. create_options |= CREATE_OPTION_READONLY;
  255. if (backup_cred(cifs_sb))
  256. create_options |= CREATE_OPEN_BACKUP_INTENT;
  257. if (tcon->ses->capabilities & CAP_NT_SMBS)
  258. rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
  259. desiredAccess, create_options,
  260. fileHandle, oplock, buf, cifs_sb->local_nls,
  261. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  262. else
  263. rc = -EIO; /* no NT SMB support fall into legacy open below */
  264. if (rc == -EIO) {
  265. /* old server, retry the open legacy style */
  266. rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
  267. desiredAccess, create_options,
  268. fileHandle, oplock, buf, cifs_sb->local_nls,
  269. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  270. }
  271. if (rc) {
  272. cFYI(1, "cifs_create returned 0x%x", rc);
  273. goto out;
  274. }
  275. /* If Open reported that we actually created a file
  276. then we now have to set the mode if possible */
  277. if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) {
  278. struct cifs_unix_set_info_args args = {
  279. .mode = mode,
  280. .ctime = NO_CHANGE_64,
  281. .atime = NO_CHANGE_64,
  282. .mtime = NO_CHANGE_64,
  283. .device = 0,
  284. };
  285. *created |= FILE_CREATED;
  286. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  287. args.uid = (__u64) current_fsuid();
  288. if (inode->i_mode & S_ISGID)
  289. args.gid = (__u64) inode->i_gid;
  290. else
  291. args.gid = (__u64) current_fsgid();
  292. } else {
  293. args.uid = NO_CHANGE_64;
  294. args.gid = NO_CHANGE_64;
  295. }
  296. CIFSSMBUnixSetFileInfo(xid, tcon, &args, *fileHandle,
  297. current->tgid);
  298. } else {
  299. /* BB implement mode setting via Windows security
  300. descriptors e.g. */
  301. /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
  302. /* Could set r/o dos attribute if mode & 0222 == 0 */
  303. }
  304. cifs_create_get_file_info:
  305. /* server might mask mode so we have to query for it */
  306. if (tcon->unix_ext)
  307. rc = cifs_get_inode_info_unix(&newinode, full_path,
  308. inode->i_sb, xid);
  309. else {
  310. rc = cifs_get_inode_info(&newinode, full_path, buf,
  311. inode->i_sb, xid, fileHandle);
  312. if (newinode) {
  313. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  314. newinode->i_mode = mode;
  315. if ((*oplock & CIFS_CREATE_ACTION) &&
  316. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) {
  317. newinode->i_uid = current_fsuid();
  318. if (inode->i_mode & S_ISGID)
  319. newinode->i_gid = inode->i_gid;
  320. else
  321. newinode->i_gid = current_fsgid();
  322. }
  323. }
  324. }
  325. cifs_create_set_dentry:
  326. if (rc != 0) {
  327. cFYI(1, "Create worked, get_inode_info failed rc = %d", rc);
  328. goto out;
  329. }
  330. d_drop(direntry);
  331. d_add(direntry, newinode);
  332. /* ENOENT for create? How weird... */
  333. rc = -ENOENT;
  334. if (!newinode) {
  335. CIFSSMBClose(xid, tcon, *fileHandle);
  336. goto out;
  337. }
  338. rc = 0;
  339. out:
  340. kfree(buf);
  341. kfree(full_path);
  342. return rc;
  343. }
  344. int
  345. cifs_atomic_open(struct inode *inode, struct dentry *direntry,
  346. struct file *file, unsigned oflags, umode_t mode,
  347. int *opened)
  348. {
  349. int rc;
  350. unsigned int xid;
  351. struct tcon_link *tlink;
  352. struct cifs_tcon *tcon;
  353. __u16 fileHandle;
  354. __u32 oplock;
  355. struct cifsFileInfo *pfile_info;
  356. /* Posix open is only called (at lookup time) for file create now. For
  357. * opens (rather than creates), because we do not know if it is a file
  358. * or directory yet, and current Samba no longer allows us to do posix
  359. * open on dirs, we could end up wasting an open call on what turns out
  360. * to be a dir. For file opens, we wait to call posix open till
  361. * cifs_open. It could be added to atomic_open in the future but the
  362. * performance tradeoff of the extra network request when EISDIR or
  363. * EACCES is returned would have to be weighed against the 50% reduction
  364. * in network traffic in the other paths.
  365. */
  366. if (!(oflags & O_CREAT)) {
  367. struct dentry *res = cifs_lookup(inode, direntry, 0);
  368. if (IS_ERR(res))
  369. return PTR_ERR(res);
  370. return finish_no_open(file, res);
  371. }
  372. rc = check_name(direntry);
  373. if (rc)
  374. return rc;
  375. xid = get_xid();
  376. cFYI(1, "parent inode = 0x%p name is: %s and dentry = 0x%p",
  377. inode, direntry->d_name.name, direntry);
  378. tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
  379. if (IS_ERR(tlink))
  380. goto out_free_xid;
  381. tcon = tlink_tcon(tlink);
  382. rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
  383. &oplock, &fileHandle, opened);
  384. if (rc)
  385. goto out;
  386. rc = finish_open(file, direntry, generic_file_open, opened);
  387. if (rc) {
  388. CIFSSMBClose(xid, tcon, fileHandle);
  389. goto out;
  390. }
  391. pfile_info = cifs_new_fileinfo(fileHandle, file, tlink, oplock);
  392. if (pfile_info == NULL) {
  393. CIFSSMBClose(xid, tcon, fileHandle);
  394. rc = -ENOMEM;
  395. }
  396. out:
  397. cifs_put_tlink(tlink);
  398. out_free_xid:
  399. free_xid(xid);
  400. return rc;
  401. }
  402. int cifs_create(struct inode *inode, struct dentry *direntry, umode_t mode,
  403. bool excl)
  404. {
  405. int rc;
  406. unsigned int xid = get_xid();
  407. /*
  408. * BB below access is probably too much for mknod to request
  409. * but we have to do query and setpathinfo so requesting
  410. * less could fail (unless we want to request getatr and setatr
  411. * permissions (only). At least for POSIX we do not have to
  412. * request so much.
  413. */
  414. unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
  415. struct tcon_link *tlink;
  416. __u16 fileHandle;
  417. __u32 oplock;
  418. int created = FILE_CREATED;
  419. cFYI(1, "cifs_create parent inode = 0x%p name is: %s and dentry = 0x%p",
  420. inode, direntry->d_name.name, direntry);
  421. tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
  422. rc = PTR_ERR(tlink);
  423. if (IS_ERR(tlink))
  424. goto out_free_xid;
  425. rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
  426. &oplock, &fileHandle, &created);
  427. if (!rc)
  428. CIFSSMBClose(xid, tlink_tcon(tlink), fileHandle);
  429. cifs_put_tlink(tlink);
  430. out_free_xid:
  431. free_xid(xid);
  432. return rc;
  433. }
  434. int cifs_mknod(struct inode *inode, struct dentry *direntry, umode_t mode,
  435. dev_t device_number)
  436. {
  437. int rc = -EPERM;
  438. unsigned int xid;
  439. int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
  440. struct cifs_sb_info *cifs_sb;
  441. struct tcon_link *tlink;
  442. struct cifs_tcon *pTcon;
  443. struct cifs_io_parms io_parms;
  444. char *full_path = NULL;
  445. struct inode *newinode = NULL;
  446. int oplock = 0;
  447. u16 fileHandle;
  448. FILE_ALL_INFO *buf = NULL;
  449. unsigned int bytes_written;
  450. struct win_dev *pdev;
  451. if (!old_valid_dev(device_number))
  452. return -EINVAL;
  453. cifs_sb = CIFS_SB(inode->i_sb);
  454. tlink = cifs_sb_tlink(cifs_sb);
  455. if (IS_ERR(tlink))
  456. return PTR_ERR(tlink);
  457. pTcon = tlink_tcon(tlink);
  458. xid = get_xid();
  459. full_path = build_path_from_dentry(direntry);
  460. if (full_path == NULL) {
  461. rc = -ENOMEM;
  462. goto mknod_out;
  463. }
  464. if (pTcon->unix_ext) {
  465. struct cifs_unix_set_info_args args = {
  466. .mode = mode & ~current_umask(),
  467. .ctime = NO_CHANGE_64,
  468. .atime = NO_CHANGE_64,
  469. .mtime = NO_CHANGE_64,
  470. .device = device_number,
  471. };
  472. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  473. args.uid = (__u64) current_fsuid();
  474. args.gid = (__u64) current_fsgid();
  475. } else {
  476. args.uid = NO_CHANGE_64;
  477. args.gid = NO_CHANGE_64;
  478. }
  479. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args,
  480. cifs_sb->local_nls,
  481. cifs_sb->mnt_cifs_flags &
  482. CIFS_MOUNT_MAP_SPECIAL_CHR);
  483. if (rc)
  484. goto mknod_out;
  485. rc = cifs_get_inode_info_unix(&newinode, full_path,
  486. inode->i_sb, xid);
  487. if (rc == 0)
  488. d_instantiate(direntry, newinode);
  489. goto mknod_out;
  490. }
  491. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
  492. goto mknod_out;
  493. cFYI(1, "sfu compat create special file");
  494. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  495. if (buf == NULL) {
  496. kfree(full_path);
  497. rc = -ENOMEM;
  498. free_xid(xid);
  499. return rc;
  500. }
  501. if (backup_cred(cifs_sb))
  502. create_options |= CREATE_OPEN_BACKUP_INTENT;
  503. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_CREATE,
  504. GENERIC_WRITE, create_options,
  505. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  506. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  507. if (rc)
  508. goto mknod_out;
  509. /* BB Do not bother to decode buf since no local inode yet to put
  510. * timestamps in, but we can reuse it safely */
  511. pdev = (struct win_dev *)buf;
  512. io_parms.netfid = fileHandle;
  513. io_parms.pid = current->tgid;
  514. io_parms.tcon = pTcon;
  515. io_parms.offset = 0;
  516. io_parms.length = sizeof(struct win_dev);
  517. if (S_ISCHR(mode)) {
  518. memcpy(pdev->type, "IntxCHR", 8);
  519. pdev->major =
  520. cpu_to_le64(MAJOR(device_number));
  521. pdev->minor =
  522. cpu_to_le64(MINOR(device_number));
  523. rc = CIFSSMBWrite(xid, &io_parms,
  524. &bytes_written, (char *)pdev,
  525. NULL, 0);
  526. } else if (S_ISBLK(mode)) {
  527. memcpy(pdev->type, "IntxBLK", 8);
  528. pdev->major =
  529. cpu_to_le64(MAJOR(device_number));
  530. pdev->minor =
  531. cpu_to_le64(MINOR(device_number));
  532. rc = CIFSSMBWrite(xid, &io_parms,
  533. &bytes_written, (char *)pdev,
  534. NULL, 0);
  535. } /* else if (S_ISFIFO) */
  536. CIFSSMBClose(xid, pTcon, fileHandle);
  537. d_drop(direntry);
  538. /* FIXME: add code here to set EAs */
  539. mknod_out:
  540. kfree(full_path);
  541. kfree(buf);
  542. free_xid(xid);
  543. cifs_put_tlink(tlink);
  544. return rc;
  545. }
  546. struct dentry *
  547. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
  548. unsigned int flags)
  549. {
  550. unsigned int xid;
  551. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  552. struct cifs_sb_info *cifs_sb;
  553. struct tcon_link *tlink;
  554. struct cifs_tcon *pTcon;
  555. struct inode *newInode = NULL;
  556. char *full_path = NULL;
  557. xid = get_xid();
  558. cFYI(1, "parent inode = 0x%p name is: %s and dentry = 0x%p",
  559. parent_dir_inode, direntry->d_name.name, direntry);
  560. /* check whether path exists */
  561. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  562. tlink = cifs_sb_tlink(cifs_sb);
  563. if (IS_ERR(tlink)) {
  564. free_xid(xid);
  565. return (struct dentry *)tlink;
  566. }
  567. pTcon = tlink_tcon(tlink);
  568. rc = check_name(direntry);
  569. if (rc)
  570. goto lookup_out;
  571. /* can not grab the rename sem here since it would
  572. deadlock in the cases (beginning of sys_rename itself)
  573. in which we already have the sb rename sem */
  574. full_path = build_path_from_dentry(direntry);
  575. if (full_path == NULL) {
  576. rc = -ENOMEM;
  577. goto lookup_out;
  578. }
  579. if (direntry->d_inode != NULL) {
  580. cFYI(1, "non-NULL inode in lookup");
  581. } else {
  582. cFYI(1, "NULL inode in lookup");
  583. }
  584. cFYI(1, "Full path: %s inode = 0x%p", full_path, direntry->d_inode);
  585. if (pTcon->unix_ext) {
  586. rc = cifs_get_inode_info_unix(&newInode, full_path,
  587. parent_dir_inode->i_sb, xid);
  588. } else {
  589. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  590. parent_dir_inode->i_sb, xid, NULL);
  591. }
  592. if ((rc == 0) && (newInode != NULL)) {
  593. d_add(direntry, newInode);
  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 == -ENOENT) {
  598. rc = 0;
  599. direntry->d_time = jiffies;
  600. d_add(direntry, NULL);
  601. /* if it was once a directory (but how can we tell?) we could do
  602. shrink_dcache_parent(direntry); */
  603. } else if (rc != -EACCES) {
  604. cERROR(1, "Unexpected lookup error %d", rc);
  605. /* We special case check for Access Denied - since that
  606. is a common return code */
  607. }
  608. lookup_out:
  609. kfree(full_path);
  610. cifs_put_tlink(tlink);
  611. free_xid(xid);
  612. return ERR_PTR(rc);
  613. }
  614. static int
  615. cifs_d_revalidate(struct dentry *direntry, unsigned int flags)
  616. {
  617. if (flags & LOOKUP_RCU)
  618. return -ECHILD;
  619. if (direntry->d_inode) {
  620. if (cifs_revalidate_dentry(direntry))
  621. return 0;
  622. else {
  623. /*
  624. * If the inode wasn't known to be a dfs entry when
  625. * the dentry was instantiated, such as when created
  626. * via ->readdir(), it needs to be set now since the
  627. * attributes will have been updated by
  628. * cifs_revalidate_dentry().
  629. */
  630. if (IS_AUTOMOUNT(direntry->d_inode) &&
  631. !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
  632. spin_lock(&direntry->d_lock);
  633. direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
  634. spin_unlock(&direntry->d_lock);
  635. }
  636. return 1;
  637. }
  638. }
  639. /*
  640. * This may be nfsd (or something), anyway, we can't see the
  641. * intent of this. So, since this can be for creation, drop it.
  642. */
  643. if (!flags)
  644. return 0;
  645. /*
  646. * Drop the negative dentry, in order to make sure to use the
  647. * case sensitive name which is specified by user if this is
  648. * for creation.
  649. */
  650. if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
  651. return 0;
  652. if (time_after(jiffies, direntry->d_time + HZ) || !lookupCacheEnabled)
  653. return 0;
  654. return 1;
  655. }
  656. /* static int cifs_d_delete(struct dentry *direntry)
  657. {
  658. int rc = 0;
  659. cFYI(1, "In cifs d_delete, name = %s", direntry->d_name.name);
  660. return rc;
  661. } */
  662. const struct dentry_operations cifs_dentry_ops = {
  663. .d_revalidate = cifs_d_revalidate,
  664. .d_automount = cifs_dfs_d_automount,
  665. /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
  666. };
  667. static int cifs_ci_hash(const struct dentry *dentry, const struct inode *inode,
  668. struct qstr *q)
  669. {
  670. struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
  671. unsigned long hash;
  672. int i;
  673. hash = init_name_hash();
  674. for (i = 0; i < q->len; i++)
  675. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  676. hash);
  677. q->hash = end_name_hash(hash);
  678. return 0;
  679. }
  680. static int cifs_ci_compare(const struct dentry *parent,
  681. const struct inode *pinode,
  682. const struct dentry *dentry, const struct inode *inode,
  683. unsigned int len, const char *str, const struct qstr *name)
  684. {
  685. struct nls_table *codepage = CIFS_SB(pinode->i_sb)->local_nls;
  686. if ((name->len == len) &&
  687. (nls_strnicmp(codepage, name->name, str, len) == 0))
  688. return 0;
  689. return 1;
  690. }
  691. const struct dentry_operations cifs_ci_dentry_ops = {
  692. .d_revalidate = cifs_d_revalidate,
  693. .d_hash = cifs_ci_hash,
  694. .d_compare = cifs_ci_compare,
  695. .d_automount = cifs_dfs_d_automount,
  696. };