dir.c 21 KB

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