dir.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 opendata *od, 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, NULL);
  370. if (IS_ERR(res))
  371. return PTR_ERR(res);
  372. finish_no_open(od, res);
  373. return 1;
  374. }
  375. rc = check_name(direntry);
  376. if (rc)
  377. return rc;
  378. xid = GetXid();
  379. cFYI(1, "parent inode = 0x%p name is: %s and dentry = 0x%p",
  380. inode, direntry->d_name.name, direntry);
  381. tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
  382. filp = ERR_CAST(tlink);
  383. if (IS_ERR(tlink))
  384. goto free_xid;
  385. tcon = tlink_tcon(tlink);
  386. rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
  387. &oplock, &fileHandle, opened);
  388. if (rc)
  389. goto out;
  390. filp = finish_open(od, direntry, generic_file_open, opened);
  391. if (IS_ERR(filp)) {
  392. rc = PTR_ERR(filp);
  393. CIFSSMBClose(xid, tcon, fileHandle);
  394. goto out;
  395. }
  396. pfile_info = cifs_new_fileinfo(fileHandle, filp, tlink, oplock);
  397. if (pfile_info == NULL) {
  398. CIFSSMBClose(xid, tcon, fileHandle);
  399. fput(filp);
  400. rc = -ENOMEM;
  401. }
  402. out:
  403. cifs_put_tlink(tlink);
  404. free_xid:
  405. FreeXid(xid);
  406. return rc;
  407. }
  408. int cifs_create(struct inode *inode, struct dentry *direntry, umode_t mode,
  409. struct nameidata *nd)
  410. {
  411. int rc;
  412. int xid = GetXid();
  413. /*
  414. * BB below access is probably too much for mknod to request
  415. * but we have to do query and setpathinfo so requesting
  416. * less could fail (unless we want to request getatr and setatr
  417. * permissions (only). At least for POSIX we do not have to
  418. * request so much.
  419. */
  420. unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
  421. struct tcon_link *tlink;
  422. __u16 fileHandle;
  423. __u32 oplock;
  424. int created = FILE_CREATED;
  425. cFYI(1, "cifs_create parent inode = 0x%p name is: %s and dentry = 0x%p",
  426. inode, direntry->d_name.name, direntry);
  427. tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
  428. rc = PTR_ERR(tlink);
  429. if (IS_ERR(tlink))
  430. goto free_xid;
  431. rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
  432. &oplock, &fileHandle, &created);
  433. if (!rc)
  434. CIFSSMBClose(xid, tlink_tcon(tlink), fileHandle);
  435. cifs_put_tlink(tlink);
  436. free_xid:
  437. FreeXid(xid);
  438. return rc;
  439. }
  440. int cifs_mknod(struct inode *inode, struct dentry *direntry, umode_t mode,
  441. dev_t device_number)
  442. {
  443. int rc = -EPERM;
  444. int xid;
  445. int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
  446. struct cifs_sb_info *cifs_sb;
  447. struct tcon_link *tlink;
  448. struct cifs_tcon *pTcon;
  449. struct cifs_io_parms io_parms;
  450. char *full_path = NULL;
  451. struct inode *newinode = NULL;
  452. int oplock = 0;
  453. u16 fileHandle;
  454. FILE_ALL_INFO *buf = NULL;
  455. unsigned int bytes_written;
  456. struct win_dev *pdev;
  457. if (!old_valid_dev(device_number))
  458. return -EINVAL;
  459. cifs_sb = CIFS_SB(inode->i_sb);
  460. tlink = cifs_sb_tlink(cifs_sb);
  461. if (IS_ERR(tlink))
  462. return PTR_ERR(tlink);
  463. pTcon = tlink_tcon(tlink);
  464. xid = GetXid();
  465. full_path = build_path_from_dentry(direntry);
  466. if (full_path == NULL) {
  467. rc = -ENOMEM;
  468. goto mknod_out;
  469. }
  470. if (pTcon->unix_ext) {
  471. struct cifs_unix_set_info_args args = {
  472. .mode = mode & ~current_umask(),
  473. .ctime = NO_CHANGE_64,
  474. .atime = NO_CHANGE_64,
  475. .mtime = NO_CHANGE_64,
  476. .device = device_number,
  477. };
  478. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  479. args.uid = (__u64) current_fsuid();
  480. args.gid = (__u64) current_fsgid();
  481. } else {
  482. args.uid = NO_CHANGE_64;
  483. args.gid = NO_CHANGE_64;
  484. }
  485. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args,
  486. cifs_sb->local_nls,
  487. cifs_sb->mnt_cifs_flags &
  488. CIFS_MOUNT_MAP_SPECIAL_CHR);
  489. if (rc)
  490. goto mknod_out;
  491. rc = cifs_get_inode_info_unix(&newinode, full_path,
  492. inode->i_sb, xid);
  493. if (rc == 0)
  494. d_instantiate(direntry, newinode);
  495. goto mknod_out;
  496. }
  497. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
  498. goto mknod_out;
  499. cFYI(1, "sfu compat create special file");
  500. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  501. if (buf == NULL) {
  502. kfree(full_path);
  503. rc = -ENOMEM;
  504. FreeXid(xid);
  505. return rc;
  506. }
  507. if (backup_cred(cifs_sb))
  508. create_options |= CREATE_OPEN_BACKUP_INTENT;
  509. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_CREATE,
  510. GENERIC_WRITE, create_options,
  511. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  512. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  513. if (rc)
  514. goto mknod_out;
  515. /* BB Do not bother to decode buf since no local inode yet to put
  516. * timestamps in, but we can reuse it safely */
  517. pdev = (struct win_dev *)buf;
  518. io_parms.netfid = fileHandle;
  519. io_parms.pid = current->tgid;
  520. io_parms.tcon = pTcon;
  521. io_parms.offset = 0;
  522. io_parms.length = sizeof(struct win_dev);
  523. if (S_ISCHR(mode)) {
  524. memcpy(pdev->type, "IntxCHR", 8);
  525. pdev->major =
  526. cpu_to_le64(MAJOR(device_number));
  527. pdev->minor =
  528. cpu_to_le64(MINOR(device_number));
  529. rc = CIFSSMBWrite(xid, &io_parms,
  530. &bytes_written, (char *)pdev,
  531. NULL, 0);
  532. } else if (S_ISBLK(mode)) {
  533. memcpy(pdev->type, "IntxBLK", 8);
  534. pdev->major =
  535. cpu_to_le64(MAJOR(device_number));
  536. pdev->minor =
  537. cpu_to_le64(MINOR(device_number));
  538. rc = CIFSSMBWrite(xid, &io_parms,
  539. &bytes_written, (char *)pdev,
  540. NULL, 0);
  541. } /* else if (S_ISFIFO) */
  542. CIFSSMBClose(xid, pTcon, fileHandle);
  543. d_drop(direntry);
  544. /* FIXME: add code here to set EAs */
  545. mknod_out:
  546. kfree(full_path);
  547. kfree(buf);
  548. FreeXid(xid);
  549. cifs_put_tlink(tlink);
  550. return rc;
  551. }
  552. struct dentry *
  553. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
  554. struct nameidata *nd)
  555. {
  556. int xid;
  557. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  558. struct cifs_sb_info *cifs_sb;
  559. struct tcon_link *tlink;
  560. struct cifs_tcon *pTcon;
  561. struct inode *newInode = NULL;
  562. char *full_path = NULL;
  563. xid = GetXid();
  564. cFYI(1, "parent inode = 0x%p name is: %s and dentry = 0x%p",
  565. parent_dir_inode, direntry->d_name.name, direntry);
  566. /* check whether path exists */
  567. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  568. tlink = cifs_sb_tlink(cifs_sb);
  569. if (IS_ERR(tlink)) {
  570. FreeXid(xid);
  571. return (struct dentry *)tlink;
  572. }
  573. pTcon = tlink_tcon(tlink);
  574. rc = check_name(direntry);
  575. if (rc)
  576. goto lookup_out;
  577. /* can not grab the rename sem here since it would
  578. deadlock in the cases (beginning of sys_rename itself)
  579. in which we already have the sb rename sem */
  580. full_path = build_path_from_dentry(direntry);
  581. if (full_path == NULL) {
  582. rc = -ENOMEM;
  583. goto lookup_out;
  584. }
  585. if (direntry->d_inode != NULL) {
  586. cFYI(1, "non-NULL inode in lookup");
  587. } else {
  588. cFYI(1, "NULL inode in lookup");
  589. }
  590. cFYI(1, "Full path: %s inode = 0x%p", full_path, direntry->d_inode);
  591. if (pTcon->unix_ext) {
  592. rc = cifs_get_inode_info_unix(&newInode, full_path,
  593. parent_dir_inode->i_sb, xid);
  594. } else {
  595. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  596. parent_dir_inode->i_sb, xid, NULL);
  597. }
  598. if ((rc == 0) && (newInode != NULL)) {
  599. d_add(direntry, newInode);
  600. /* since paths are not looked up by component - the parent
  601. directories are presumed to be good here */
  602. renew_parental_timestamps(direntry);
  603. } else if (rc == -ENOENT) {
  604. rc = 0;
  605. direntry->d_time = jiffies;
  606. d_add(direntry, NULL);
  607. /* if it was once a directory (but how can we tell?) we could do
  608. shrink_dcache_parent(direntry); */
  609. } else if (rc != -EACCES) {
  610. cERROR(1, "Unexpected lookup error %d", rc);
  611. /* We special case check for Access Denied - since that
  612. is a common return code */
  613. }
  614. lookup_out:
  615. kfree(full_path);
  616. cifs_put_tlink(tlink);
  617. FreeXid(xid);
  618. return ERR_PTR(rc);
  619. }
  620. static int
  621. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  622. {
  623. if (nd && (nd->flags & LOOKUP_RCU))
  624. return -ECHILD;
  625. if (direntry->d_inode) {
  626. if (cifs_revalidate_dentry(direntry))
  627. return 0;
  628. else {
  629. /*
  630. * If the inode wasn't known to be a dfs entry when
  631. * the dentry was instantiated, such as when created
  632. * via ->readdir(), it needs to be set now since the
  633. * attributes will have been updated by
  634. * cifs_revalidate_dentry().
  635. */
  636. if (IS_AUTOMOUNT(direntry->d_inode) &&
  637. !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
  638. spin_lock(&direntry->d_lock);
  639. direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
  640. spin_unlock(&direntry->d_lock);
  641. }
  642. return 1;
  643. }
  644. }
  645. /*
  646. * This may be nfsd (or something), anyway, we can't see the
  647. * intent of this. So, since this can be for creation, drop it.
  648. */
  649. if (!nd)
  650. return 0;
  651. /*
  652. * Drop the negative dentry, in order to make sure to use the
  653. * case sensitive name which is specified by user if this is
  654. * for creation.
  655. */
  656. if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
  657. return 0;
  658. if (time_after(jiffies, direntry->d_time + HZ) || !lookupCacheEnabled)
  659. return 0;
  660. return 1;
  661. }
  662. /* static int cifs_d_delete(struct dentry *direntry)
  663. {
  664. int rc = 0;
  665. cFYI(1, "In cifs d_delete, name = %s", direntry->d_name.name);
  666. return rc;
  667. } */
  668. const struct dentry_operations cifs_dentry_ops = {
  669. .d_revalidate = cifs_d_revalidate,
  670. .d_automount = cifs_dfs_d_automount,
  671. /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
  672. };
  673. static int cifs_ci_hash(const struct dentry *dentry, const struct inode *inode,
  674. struct qstr *q)
  675. {
  676. struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
  677. unsigned long hash;
  678. int i;
  679. hash = init_name_hash();
  680. for (i = 0; i < q->len; i++)
  681. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  682. hash);
  683. q->hash = end_name_hash(hash);
  684. return 0;
  685. }
  686. static int cifs_ci_compare(const struct dentry *parent,
  687. const struct inode *pinode,
  688. const struct dentry *dentry, const struct inode *inode,
  689. unsigned int len, const char *str, const struct qstr *name)
  690. {
  691. struct nls_table *codepage = CIFS_SB(pinode->i_sb)->local_nls;
  692. if ((name->len == len) &&
  693. (nls_strnicmp(codepage, name->name, str, len) == 0))
  694. return 0;
  695. return 1;
  696. }
  697. const struct dentry_operations cifs_ci_dentry_ops = {
  698. .d_revalidate = cifs_d_revalidate,
  699. .d_hash = cifs_ci_hash,
  700. .d_compare = cifs_ci_compare,
  701. .d_automount = cifs_dfs_d_automount,
  702. };