dir.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. char *
  46. cifs_build_path_to_root(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
  47. struct cifs_tcon *tcon)
  48. {
  49. int pplen = vol->prepath ? strlen(vol->prepath) : 0;
  50. int dfsplen;
  51. char *full_path = NULL;
  52. /* if no prefix path, simply set path to the root of share to "" */
  53. if (pplen == 0) {
  54. full_path = kzalloc(1, GFP_KERNEL);
  55. return full_path;
  56. }
  57. if (tcon->Flags & SMB_SHARE_IS_IN_DFS)
  58. dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
  59. else
  60. dfsplen = 0;
  61. full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
  62. if (full_path == NULL)
  63. return full_path;
  64. if (dfsplen)
  65. strncpy(full_path, tcon->treeName, dfsplen);
  66. strncpy(full_path + dfsplen, vol->prepath, pplen);
  67. convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
  68. full_path[dfsplen + pplen] = 0; /* add trailing null */
  69. return full_path;
  70. }
  71. /* Note: caller must free return buffer */
  72. char *
  73. build_path_from_dentry(struct dentry *direntry)
  74. {
  75. struct dentry *temp;
  76. int namelen;
  77. int dfsplen;
  78. char *full_path;
  79. char dirsep;
  80. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  81. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  82. unsigned seq;
  83. dirsep = CIFS_DIR_SEP(cifs_sb);
  84. if (tcon->Flags & SMB_SHARE_IS_IN_DFS)
  85. dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
  86. else
  87. dfsplen = 0;
  88. cifs_bp_rename_retry:
  89. namelen = dfsplen;
  90. seq = read_seqbegin(&rename_lock);
  91. rcu_read_lock();
  92. for (temp = direntry; !IS_ROOT(temp);) {
  93. namelen += (1 + temp->d_name.len);
  94. temp = temp->d_parent;
  95. if (temp == NULL) {
  96. cERROR(1, "corrupt dentry");
  97. rcu_read_unlock();
  98. return NULL;
  99. }
  100. }
  101. rcu_read_unlock();
  102. full_path = kmalloc(namelen+1, GFP_KERNEL);
  103. if (full_path == NULL)
  104. return full_path;
  105. full_path[namelen] = 0; /* trailing null */
  106. rcu_read_lock();
  107. for (temp = direntry; !IS_ROOT(temp);) {
  108. spin_lock(&temp->d_lock);
  109. namelen -= 1 + temp->d_name.len;
  110. if (namelen < 0) {
  111. spin_unlock(&temp->d_lock);
  112. break;
  113. } else {
  114. full_path[namelen] = dirsep;
  115. strncpy(full_path + namelen + 1, temp->d_name.name,
  116. temp->d_name.len);
  117. cFYI(0, "name: %s", full_path + namelen);
  118. }
  119. spin_unlock(&temp->d_lock);
  120. temp = temp->d_parent;
  121. if (temp == NULL) {
  122. cERROR(1, "corrupt dentry");
  123. rcu_read_unlock();
  124. kfree(full_path);
  125. return NULL;
  126. }
  127. }
  128. rcu_read_unlock();
  129. if (namelen != dfsplen || read_seqretry(&rename_lock, seq)) {
  130. cFYI(1, "did not end path lookup where expected. namelen=%d "
  131. "dfsplen=%d", namelen, dfsplen);
  132. /* presumably this is only possible if racing with a rename
  133. of one of the parent directories (we can not lock the dentries
  134. above us to prevent this, but retrying should be harmless) */
  135. kfree(full_path);
  136. goto cifs_bp_rename_retry;
  137. }
  138. /* DIR_SEP already set for byte 0 / vs \ but not for
  139. subsequent slashes in prepath which currently must
  140. be entered the right way - not sure if there is an alternative
  141. since the '\' is a valid posix character so we can not switch
  142. those safely to '/' if any are found in the middle of the prepath */
  143. /* BB test paths to Windows with '/' in the midst of prepath */
  144. if (dfsplen) {
  145. strncpy(full_path, tcon->treeName, dfsplen);
  146. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
  147. int i;
  148. for (i = 0; i < dfsplen; i++) {
  149. if (full_path[i] == '\\')
  150. full_path[i] = '/';
  151. }
  152. }
  153. }
  154. return full_path;
  155. }
  156. /*
  157. * Don't allow the separator character in a path component.
  158. * The VFS will not allow "/", but "\" is allowed by posix.
  159. */
  160. static int
  161. check_name(struct dentry *direntry)
  162. {
  163. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  164. int i;
  165. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
  166. for (i = 0; i < direntry->d_name.len; i++) {
  167. if (direntry->d_name.name[i] == '\\') {
  168. cFYI(1, "Invalid file name");
  169. return -EINVAL;
  170. }
  171. }
  172. }
  173. return 0;
  174. }
  175. /* Inode operations in similar order to how they appear in Linux file fs.h */
  176. static int
  177. cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid,
  178. struct tcon_link *tlink, unsigned oflags, umode_t mode,
  179. __u32 *oplock, struct cifs_fid *fid, int *created)
  180. {
  181. int rc = -ENOENT;
  182. int create_options = CREATE_NOT_DIR;
  183. int desired_access;
  184. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  185. struct cifs_tcon *tcon = tlink_tcon(tlink);
  186. char *full_path = NULL;
  187. FILE_ALL_INFO *buf = NULL;
  188. struct inode *newinode = NULL;
  189. int disposition;
  190. struct TCP_Server_Info *server = tcon->ses->server;
  191. *oplock = 0;
  192. if (tcon->ses->server->oplocks)
  193. *oplock = REQ_OPLOCK;
  194. full_path = build_path_from_dentry(direntry);
  195. if (full_path == NULL) {
  196. rc = -ENOMEM;
  197. goto out;
  198. }
  199. if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open &&
  200. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  201. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  202. rc = cifs_posix_open(full_path, &newinode, inode->i_sb, mode,
  203. oflags, oplock, &fid->netfid, xid);
  204. switch (rc) {
  205. case 0:
  206. if (newinode == NULL) {
  207. /* query inode info */
  208. goto cifs_create_get_file_info;
  209. }
  210. if (!S_ISREG(newinode->i_mode)) {
  211. /*
  212. * The server may allow us to open things like
  213. * FIFOs, but the client isn't set up to deal
  214. * with that. If it's not a regular file, just
  215. * close it and proceed as if it were a normal
  216. * lookup.
  217. */
  218. CIFSSMBClose(xid, tcon, fid->netfid);
  219. goto cifs_create_get_file_info;
  220. }
  221. /* success, no need to query */
  222. goto cifs_create_set_dentry;
  223. case -ENOENT:
  224. goto cifs_create_get_file_info;
  225. case -EIO:
  226. case -EINVAL:
  227. /*
  228. * EIO could indicate that (posix open) operation is not
  229. * supported, despite what server claimed in capability
  230. * negotiation.
  231. *
  232. * POSIX open in samba versions 3.3.1 and earlier could
  233. * incorrectly fail with invalid parameter.
  234. */
  235. tcon->broken_posix_open = true;
  236. break;
  237. case -EREMOTE:
  238. case -EOPNOTSUPP:
  239. /*
  240. * EREMOTE indicates DFS junction, which is not handled
  241. * in posix open. If either that or op not supported
  242. * returned, follow the normal lookup.
  243. */
  244. break;
  245. default:
  246. goto out;
  247. }
  248. /*
  249. * fallthrough to retry, using older open call, this is case
  250. * where server does not support this SMB level, and falsely
  251. * claims capability (also get here for DFS case which should be
  252. * rare for path not covered on files)
  253. */
  254. }
  255. desired_access = 0;
  256. if (OPEN_FMODE(oflags) & FMODE_READ)
  257. desired_access |= GENERIC_READ; /* is this too little? */
  258. if (OPEN_FMODE(oflags) & FMODE_WRITE)
  259. desired_access |= GENERIC_WRITE;
  260. disposition = FILE_OVERWRITE_IF;
  261. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  262. disposition = FILE_CREATE;
  263. else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  264. disposition = FILE_OVERWRITE_IF;
  265. else if ((oflags & O_CREAT) == O_CREAT)
  266. disposition = FILE_OPEN_IF;
  267. else
  268. cFYI(1, "Create flag not set in create function");
  269. /*
  270. * BB add processing to set equivalent of mode - e.g. via CreateX with
  271. * ACLs
  272. */
  273. if (!server->ops->open) {
  274. rc = -ENOSYS;
  275. goto out;
  276. }
  277. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  278. if (buf == NULL) {
  279. rc = -ENOMEM;
  280. goto out;
  281. }
  282. /*
  283. * if we're not using unix extensions, see if we need to set
  284. * ATTR_READONLY on the create call
  285. */
  286. if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
  287. create_options |= CREATE_OPTION_READONLY;
  288. if (backup_cred(cifs_sb))
  289. create_options |= CREATE_OPEN_BACKUP_INTENT;
  290. rc = server->ops->open(xid, tcon, full_path, disposition,
  291. desired_access, create_options, fid, oplock,
  292. buf, cifs_sb);
  293. if (rc) {
  294. cFYI(1, "cifs_create returned 0x%x", rc);
  295. goto out;
  296. }
  297. /*
  298. * If Open reported that we actually created a file then we now have to
  299. * set the mode if possible.
  300. */
  301. if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) {
  302. struct cifs_unix_set_info_args args = {
  303. .mode = mode,
  304. .ctime = NO_CHANGE_64,
  305. .atime = NO_CHANGE_64,
  306. .mtime = NO_CHANGE_64,
  307. .device = 0,
  308. };
  309. *created |= FILE_CREATED;
  310. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  311. args.uid = (__u64) current_fsuid();
  312. if (inode->i_mode & S_ISGID)
  313. args.gid = (__u64) inode->i_gid;
  314. else
  315. args.gid = (__u64) current_fsgid();
  316. } else {
  317. args.uid = NO_CHANGE_64;
  318. args.gid = NO_CHANGE_64;
  319. }
  320. CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid,
  321. current->tgid);
  322. } else {
  323. /*
  324. * BB implement mode setting via Windows security
  325. * descriptors e.g.
  326. */
  327. /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
  328. /* Could set r/o dos attribute if mode & 0222 == 0 */
  329. }
  330. cifs_create_get_file_info:
  331. /* server might mask mode so we have to query for it */
  332. if (tcon->unix_ext)
  333. rc = cifs_get_inode_info_unix(&newinode, full_path, inode->i_sb,
  334. xid);
  335. else {
  336. rc = cifs_get_inode_info(&newinode, full_path, buf, inode->i_sb,
  337. xid, &fid->netfid);
  338. if (newinode) {
  339. if (server->ops->set_lease_key)
  340. server->ops->set_lease_key(newinode, fid);
  341. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  342. newinode->i_mode = mode;
  343. if ((*oplock & CIFS_CREATE_ACTION) &&
  344. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) {
  345. newinode->i_uid = current_fsuid();
  346. if (inode->i_mode & S_ISGID)
  347. newinode->i_gid = inode->i_gid;
  348. else
  349. newinode->i_gid = current_fsgid();
  350. }
  351. }
  352. }
  353. cifs_create_set_dentry:
  354. if (rc != 0) {
  355. cFYI(1, "Create worked, get_inode_info failed rc = %d", rc);
  356. if (server->ops->close)
  357. server->ops->close(xid, tcon, fid);
  358. goto out;
  359. }
  360. d_drop(direntry);
  361. d_add(direntry, newinode);
  362. out:
  363. kfree(buf);
  364. kfree(full_path);
  365. return rc;
  366. }
  367. int
  368. cifs_atomic_open(struct inode *inode, struct dentry *direntry,
  369. struct file *file, unsigned oflags, umode_t mode,
  370. int *opened)
  371. {
  372. int rc;
  373. unsigned int xid;
  374. struct tcon_link *tlink;
  375. struct cifs_tcon *tcon;
  376. struct TCP_Server_Info *server;
  377. struct cifs_fid fid;
  378. struct cifs_pending_open open;
  379. __u32 oplock;
  380. struct cifsFileInfo *file_info;
  381. /*
  382. * Posix open is only called (at lookup time) for file create now. For
  383. * opens (rather than creates), because we do not know if it is a file
  384. * or directory yet, and current Samba no longer allows us to do posix
  385. * open on dirs, we could end up wasting an open call on what turns out
  386. * to be a dir. For file opens, we wait to call posix open till
  387. * cifs_open. It could be added to atomic_open in the future but the
  388. * performance tradeoff of the extra network request when EISDIR or
  389. * EACCES is returned would have to be weighed against the 50% reduction
  390. * in network traffic in the other paths.
  391. */
  392. if (!(oflags & O_CREAT)) {
  393. struct dentry *res;
  394. /*
  395. * Check for hashed negative dentry. We have already revalidated
  396. * the dentry and it is fine. No need to perform another lookup.
  397. */
  398. if (!d_unhashed(direntry))
  399. return -ENOENT;
  400. res = cifs_lookup(inode, direntry, 0);
  401. if (IS_ERR(res))
  402. return PTR_ERR(res);
  403. return finish_no_open(file, res);
  404. }
  405. rc = check_name(direntry);
  406. if (rc)
  407. return rc;
  408. xid = get_xid();
  409. cFYI(1, "parent inode = 0x%p name is: %s and dentry = 0x%p",
  410. inode, direntry->d_name.name, direntry);
  411. tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
  412. if (IS_ERR(tlink))
  413. goto out_free_xid;
  414. tcon = tlink_tcon(tlink);
  415. server = tcon->ses->server;
  416. if (server->ops->new_lease_key)
  417. server->ops->new_lease_key(&fid);
  418. cifs_add_pending_open(&fid, tlink, &open);
  419. rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
  420. &oplock, &fid, opened);
  421. if (rc) {
  422. cifs_del_pending_open(&open);
  423. goto out;
  424. }
  425. rc = finish_open(file, direntry, generic_file_open, opened);
  426. if (rc) {
  427. if (server->ops->close)
  428. server->ops->close(xid, tcon, &fid);
  429. cifs_del_pending_open(&open);
  430. goto out;
  431. }
  432. file_info = cifs_new_fileinfo(&fid, file, tlink, oplock);
  433. if (file_info == NULL) {
  434. if (server->ops->close)
  435. server->ops->close(xid, tcon, &fid);
  436. cifs_del_pending_open(&open);
  437. rc = -ENOMEM;
  438. }
  439. out:
  440. cifs_put_tlink(tlink);
  441. out_free_xid:
  442. free_xid(xid);
  443. return rc;
  444. }
  445. int cifs_create(struct inode *inode, struct dentry *direntry, umode_t mode,
  446. bool excl)
  447. {
  448. int rc;
  449. unsigned int xid = get_xid();
  450. /*
  451. * BB below access is probably too much for mknod to request
  452. * but we have to do query and setpathinfo so requesting
  453. * less could fail (unless we want to request getatr and setatr
  454. * permissions (only). At least for POSIX we do not have to
  455. * request so much.
  456. */
  457. unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
  458. struct tcon_link *tlink;
  459. struct cifs_tcon *tcon;
  460. struct TCP_Server_Info *server;
  461. struct cifs_fid fid;
  462. __u32 oplock;
  463. int created = FILE_CREATED;
  464. cFYI(1, "cifs_create parent inode = 0x%p name is: %s and dentry = 0x%p",
  465. inode, direntry->d_name.name, direntry);
  466. tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
  467. rc = PTR_ERR(tlink);
  468. if (IS_ERR(tlink))
  469. goto out_free_xid;
  470. tcon = tlink_tcon(tlink);
  471. server = tcon->ses->server;
  472. if (server->ops->new_lease_key)
  473. server->ops->new_lease_key(&fid);
  474. rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
  475. &oplock, &fid, &created);
  476. if (!rc && server->ops->close)
  477. server->ops->close(xid, tcon, &fid);
  478. cifs_put_tlink(tlink);
  479. out_free_xid:
  480. free_xid(xid);
  481. return rc;
  482. }
  483. int cifs_mknod(struct inode *inode, struct dentry *direntry, umode_t mode,
  484. dev_t device_number)
  485. {
  486. int rc = -EPERM;
  487. unsigned int xid;
  488. int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
  489. struct cifs_sb_info *cifs_sb;
  490. struct tcon_link *tlink;
  491. struct cifs_tcon *pTcon;
  492. struct cifs_io_parms io_parms;
  493. char *full_path = NULL;
  494. struct inode *newinode = NULL;
  495. int oplock = 0;
  496. u16 fileHandle;
  497. FILE_ALL_INFO *buf = NULL;
  498. unsigned int bytes_written;
  499. struct win_dev *pdev;
  500. if (!old_valid_dev(device_number))
  501. return -EINVAL;
  502. cifs_sb = CIFS_SB(inode->i_sb);
  503. tlink = cifs_sb_tlink(cifs_sb);
  504. if (IS_ERR(tlink))
  505. return PTR_ERR(tlink);
  506. pTcon = tlink_tcon(tlink);
  507. xid = get_xid();
  508. full_path = build_path_from_dentry(direntry);
  509. if (full_path == NULL) {
  510. rc = -ENOMEM;
  511. goto mknod_out;
  512. }
  513. if (pTcon->unix_ext) {
  514. struct cifs_unix_set_info_args args = {
  515. .mode = mode & ~current_umask(),
  516. .ctime = NO_CHANGE_64,
  517. .atime = NO_CHANGE_64,
  518. .mtime = NO_CHANGE_64,
  519. .device = device_number,
  520. };
  521. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  522. args.uid = (__u64) current_fsuid();
  523. args.gid = (__u64) current_fsgid();
  524. } else {
  525. args.uid = NO_CHANGE_64;
  526. args.gid = NO_CHANGE_64;
  527. }
  528. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args,
  529. cifs_sb->local_nls,
  530. cifs_sb->mnt_cifs_flags &
  531. CIFS_MOUNT_MAP_SPECIAL_CHR);
  532. if (rc)
  533. goto mknod_out;
  534. rc = cifs_get_inode_info_unix(&newinode, full_path,
  535. inode->i_sb, xid);
  536. if (rc == 0)
  537. d_instantiate(direntry, newinode);
  538. goto mknod_out;
  539. }
  540. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
  541. goto mknod_out;
  542. cFYI(1, "sfu compat create special file");
  543. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  544. if (buf == NULL) {
  545. kfree(full_path);
  546. rc = -ENOMEM;
  547. free_xid(xid);
  548. return rc;
  549. }
  550. if (backup_cred(cifs_sb))
  551. create_options |= CREATE_OPEN_BACKUP_INTENT;
  552. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_CREATE,
  553. GENERIC_WRITE, create_options,
  554. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  555. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  556. if (rc)
  557. goto mknod_out;
  558. /* BB Do not bother to decode buf since no local inode yet to put
  559. * timestamps in, but we can reuse it safely */
  560. pdev = (struct win_dev *)buf;
  561. io_parms.netfid = fileHandle;
  562. io_parms.pid = current->tgid;
  563. io_parms.tcon = pTcon;
  564. io_parms.offset = 0;
  565. io_parms.length = sizeof(struct win_dev);
  566. if (S_ISCHR(mode)) {
  567. memcpy(pdev->type, "IntxCHR", 8);
  568. pdev->major =
  569. cpu_to_le64(MAJOR(device_number));
  570. pdev->minor =
  571. cpu_to_le64(MINOR(device_number));
  572. rc = CIFSSMBWrite(xid, &io_parms,
  573. &bytes_written, (char *)pdev,
  574. NULL, 0);
  575. } else if (S_ISBLK(mode)) {
  576. memcpy(pdev->type, "IntxBLK", 8);
  577. pdev->major =
  578. cpu_to_le64(MAJOR(device_number));
  579. pdev->minor =
  580. cpu_to_le64(MINOR(device_number));
  581. rc = CIFSSMBWrite(xid, &io_parms,
  582. &bytes_written, (char *)pdev,
  583. NULL, 0);
  584. } /* else if (S_ISFIFO) */
  585. CIFSSMBClose(xid, pTcon, fileHandle);
  586. d_drop(direntry);
  587. /* FIXME: add code here to set EAs */
  588. mknod_out:
  589. kfree(full_path);
  590. kfree(buf);
  591. free_xid(xid);
  592. cifs_put_tlink(tlink);
  593. return rc;
  594. }
  595. struct dentry *
  596. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
  597. unsigned int flags)
  598. {
  599. unsigned int xid;
  600. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  601. struct cifs_sb_info *cifs_sb;
  602. struct tcon_link *tlink;
  603. struct cifs_tcon *pTcon;
  604. struct inode *newInode = NULL;
  605. char *full_path = NULL;
  606. xid = get_xid();
  607. cFYI(1, "parent inode = 0x%p name is: %s and dentry = 0x%p",
  608. parent_dir_inode, direntry->d_name.name, direntry);
  609. /* check whether path exists */
  610. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  611. tlink = cifs_sb_tlink(cifs_sb);
  612. if (IS_ERR(tlink)) {
  613. free_xid(xid);
  614. return (struct dentry *)tlink;
  615. }
  616. pTcon = tlink_tcon(tlink);
  617. rc = check_name(direntry);
  618. if (rc)
  619. goto lookup_out;
  620. /* can not grab the rename sem here since it would
  621. deadlock in the cases (beginning of sys_rename itself)
  622. in which we already have the sb rename sem */
  623. full_path = build_path_from_dentry(direntry);
  624. if (full_path == NULL) {
  625. rc = -ENOMEM;
  626. goto lookup_out;
  627. }
  628. if (direntry->d_inode != NULL) {
  629. cFYI(1, "non-NULL inode in lookup");
  630. } else {
  631. cFYI(1, "NULL inode in lookup");
  632. }
  633. cFYI(1, "Full path: %s inode = 0x%p", full_path, direntry->d_inode);
  634. if (pTcon->unix_ext) {
  635. rc = cifs_get_inode_info_unix(&newInode, full_path,
  636. parent_dir_inode->i_sb, xid);
  637. } else {
  638. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  639. parent_dir_inode->i_sb, xid, NULL);
  640. }
  641. if ((rc == 0) && (newInode != NULL)) {
  642. d_add(direntry, newInode);
  643. /* since paths are not looked up by component - the parent
  644. directories are presumed to be good here */
  645. renew_parental_timestamps(direntry);
  646. } else if (rc == -ENOENT) {
  647. rc = 0;
  648. direntry->d_time = jiffies;
  649. d_add(direntry, NULL);
  650. /* if it was once a directory (but how can we tell?) we could do
  651. shrink_dcache_parent(direntry); */
  652. } else if (rc != -EACCES) {
  653. cERROR(1, "Unexpected lookup error %d", rc);
  654. /* We special case check for Access Denied - since that
  655. is a common return code */
  656. }
  657. lookup_out:
  658. kfree(full_path);
  659. cifs_put_tlink(tlink);
  660. free_xid(xid);
  661. return ERR_PTR(rc);
  662. }
  663. static int
  664. cifs_d_revalidate(struct dentry *direntry, unsigned int flags)
  665. {
  666. if (flags & LOOKUP_RCU)
  667. return -ECHILD;
  668. if (direntry->d_inode) {
  669. if (cifs_revalidate_dentry(direntry))
  670. return 0;
  671. else {
  672. /*
  673. * If the inode wasn't known to be a dfs entry when
  674. * the dentry was instantiated, such as when created
  675. * via ->readdir(), it needs to be set now since the
  676. * attributes will have been updated by
  677. * cifs_revalidate_dentry().
  678. */
  679. if (IS_AUTOMOUNT(direntry->d_inode) &&
  680. !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
  681. spin_lock(&direntry->d_lock);
  682. direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
  683. spin_unlock(&direntry->d_lock);
  684. }
  685. return 1;
  686. }
  687. }
  688. /*
  689. * This may be nfsd (or something), anyway, we can't see the
  690. * intent of this. So, since this can be for creation, drop it.
  691. */
  692. if (!flags)
  693. return 0;
  694. /*
  695. * Drop the negative dentry, in order to make sure to use the
  696. * case sensitive name which is specified by user if this is
  697. * for creation.
  698. */
  699. if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
  700. return 0;
  701. if (time_after(jiffies, direntry->d_time + HZ) || !lookupCacheEnabled)
  702. return 0;
  703. return 1;
  704. }
  705. /* static int cifs_d_delete(struct dentry *direntry)
  706. {
  707. int rc = 0;
  708. cFYI(1, "In cifs d_delete, name = %s", direntry->d_name.name);
  709. return rc;
  710. } */
  711. const struct dentry_operations cifs_dentry_ops = {
  712. .d_revalidate = cifs_d_revalidate,
  713. .d_automount = cifs_dfs_d_automount,
  714. /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
  715. };
  716. static int cifs_ci_hash(const struct dentry *dentry, const struct inode *inode,
  717. struct qstr *q)
  718. {
  719. struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
  720. unsigned long hash;
  721. int i;
  722. hash = init_name_hash();
  723. for (i = 0; i < q->len; i++)
  724. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  725. hash);
  726. q->hash = end_name_hash(hash);
  727. return 0;
  728. }
  729. static int cifs_ci_compare(const struct dentry *parent,
  730. const struct inode *pinode,
  731. const struct dentry *dentry, const struct inode *inode,
  732. unsigned int len, const char *str, const struct qstr *name)
  733. {
  734. struct nls_table *codepage = CIFS_SB(pinode->i_sb)->local_nls;
  735. if ((name->len == len) &&
  736. (nls_strnicmp(codepage, name->name, str, len) == 0))
  737. return 0;
  738. return 1;
  739. }
  740. const struct dentry_operations cifs_ci_dentry_ops = {
  741. .d_revalidate = cifs_d_revalidate,
  742. .d_hash = cifs_ci_hash,
  743. .d_compare = cifs_ci_compare,
  744. .d_automount = cifs_dfs_d_automount,
  745. };