dir.c 20 KB

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