dir.c 22 KB

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