dir.c 22 KB

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