dir.c 21 KB

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