dir.c 21 KB

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