dir.c 21 KB

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