dir.c 22 KB

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