dir.c 21 KB

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