dir.c 23 KB

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