dir.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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)
  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. 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);
  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: %pd and dentry = 0x%p\n",
  419. inode, direntry, 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);
  432. if (rc) {
  433. cifs_del_pending_open(&open);
  434. goto out;
  435. }
  436. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  437. *opened |= FILE_CREATED;
  438. rc = finish_open(file, direntry, generic_file_open, opened);
  439. if (rc) {
  440. if (server->ops->close)
  441. server->ops->close(xid, tcon, &fid);
  442. cifs_del_pending_open(&open);
  443. goto out;
  444. }
  445. if (file->f_flags & O_DIRECT &&
  446. CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
  447. if (CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  448. file->f_op = &cifs_file_direct_nobrl_ops;
  449. else
  450. file->f_op = &cifs_file_direct_ops;
  451. }
  452. file_info = cifs_new_fileinfo(&fid, file, tlink, oplock);
  453. if (file_info == NULL) {
  454. if (server->ops->close)
  455. server->ops->close(xid, tcon, &fid);
  456. cifs_del_pending_open(&open);
  457. fput(file);
  458. rc = -ENOMEM;
  459. }
  460. out:
  461. cifs_put_tlink(tlink);
  462. out_free_xid:
  463. free_xid(xid);
  464. return rc;
  465. }
  466. int cifs_create(struct inode *inode, struct dentry *direntry, umode_t mode,
  467. bool excl)
  468. {
  469. int rc;
  470. unsigned int xid = get_xid();
  471. /*
  472. * BB below access is probably too much for mknod to request
  473. * but we have to do query and setpathinfo so requesting
  474. * less could fail (unless we want to request getatr and setatr
  475. * permissions (only). At least for POSIX we do not have to
  476. * request so much.
  477. */
  478. unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
  479. struct tcon_link *tlink;
  480. struct cifs_tcon *tcon;
  481. struct TCP_Server_Info *server;
  482. struct cifs_fid fid;
  483. __u32 oplock;
  484. cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
  485. inode, direntry, direntry);
  486. tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
  487. rc = PTR_ERR(tlink);
  488. if (IS_ERR(tlink))
  489. goto out_free_xid;
  490. tcon = tlink_tcon(tlink);
  491. server = tcon->ses->server;
  492. if (server->ops->new_lease_key)
  493. server->ops->new_lease_key(&fid);
  494. rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
  495. &oplock, &fid);
  496. if (!rc && server->ops->close)
  497. server->ops->close(xid, tcon, &fid);
  498. cifs_put_tlink(tlink);
  499. out_free_xid:
  500. free_xid(xid);
  501. return rc;
  502. }
  503. int cifs_mknod(struct inode *inode, struct dentry *direntry, umode_t mode,
  504. dev_t device_number)
  505. {
  506. int rc = -EPERM;
  507. unsigned int xid;
  508. int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
  509. struct cifs_sb_info *cifs_sb;
  510. struct tcon_link *tlink;
  511. struct cifs_tcon *tcon;
  512. struct cifs_io_parms io_parms;
  513. char *full_path = NULL;
  514. struct inode *newinode = NULL;
  515. __u32 oplock = 0;
  516. struct cifs_fid fid;
  517. struct cifs_open_parms oparms;
  518. FILE_ALL_INFO *buf = NULL;
  519. unsigned int bytes_written;
  520. struct win_dev *pdev;
  521. struct kvec iov[2];
  522. if (!old_valid_dev(device_number))
  523. return -EINVAL;
  524. cifs_sb = CIFS_SB(inode->i_sb);
  525. tlink = cifs_sb_tlink(cifs_sb);
  526. if (IS_ERR(tlink))
  527. return PTR_ERR(tlink);
  528. tcon = tlink_tcon(tlink);
  529. xid = get_xid();
  530. full_path = build_path_from_dentry(direntry);
  531. if (full_path == NULL) {
  532. rc = -ENOMEM;
  533. goto mknod_out;
  534. }
  535. if (tcon->unix_ext) {
  536. struct cifs_unix_set_info_args args = {
  537. .mode = mode & ~current_umask(),
  538. .ctime = NO_CHANGE_64,
  539. .atime = NO_CHANGE_64,
  540. .mtime = NO_CHANGE_64,
  541. .device = device_number,
  542. };
  543. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  544. args.uid = current_fsuid();
  545. args.gid = current_fsgid();
  546. } else {
  547. args.uid = INVALID_UID; /* no change */
  548. args.gid = INVALID_GID; /* no change */
  549. }
  550. rc = CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
  551. cifs_sb->local_nls,
  552. cifs_remap(cifs_sb));
  553. if (rc)
  554. goto mknod_out;
  555. rc = cifs_get_inode_info_unix(&newinode, full_path,
  556. inode->i_sb, xid);
  557. if (rc == 0)
  558. d_instantiate(direntry, newinode);
  559. goto mknod_out;
  560. }
  561. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
  562. goto mknod_out;
  563. cifs_dbg(FYI, "sfu compat create special file\n");
  564. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  565. if (buf == NULL) {
  566. kfree(full_path);
  567. rc = -ENOMEM;
  568. free_xid(xid);
  569. return rc;
  570. }
  571. if (backup_cred(cifs_sb))
  572. create_options |= CREATE_OPEN_BACKUP_INTENT;
  573. oparms.tcon = tcon;
  574. oparms.cifs_sb = cifs_sb;
  575. oparms.desired_access = GENERIC_WRITE;
  576. oparms.create_options = create_options;
  577. oparms.disposition = FILE_CREATE;
  578. oparms.path = full_path;
  579. oparms.fid = &fid;
  580. oparms.reconnect = false;
  581. if (tcon->ses->server->oplocks)
  582. oplock = REQ_OPLOCK;
  583. else
  584. oplock = 0;
  585. rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
  586. if (rc)
  587. goto mknod_out;
  588. /*
  589. * BB Do not bother to decode buf since no local inode yet to put
  590. * timestamps in, but we can reuse it safely.
  591. */
  592. pdev = (struct win_dev *)buf;
  593. io_parms.pid = current->tgid;
  594. io_parms.tcon = tcon;
  595. io_parms.offset = 0;
  596. io_parms.length = sizeof(struct win_dev);
  597. iov[1].iov_base = buf;
  598. iov[1].iov_len = sizeof(struct win_dev);
  599. if (S_ISCHR(mode)) {
  600. memcpy(pdev->type, "IntxCHR", 8);
  601. pdev->major = cpu_to_le64(MAJOR(device_number));
  602. pdev->minor = cpu_to_le64(MINOR(device_number));
  603. rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
  604. &bytes_written, iov, 1);
  605. } else if (S_ISBLK(mode)) {
  606. memcpy(pdev->type, "IntxBLK", 8);
  607. pdev->major = cpu_to_le64(MAJOR(device_number));
  608. pdev->minor = cpu_to_le64(MINOR(device_number));
  609. rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
  610. &bytes_written, iov, 1);
  611. } /* else if (S_ISFIFO) */
  612. tcon->ses->server->ops->close(xid, tcon, &fid);
  613. d_drop(direntry);
  614. /* FIXME: add code here to set EAs */
  615. mknod_out:
  616. kfree(full_path);
  617. kfree(buf);
  618. free_xid(xid);
  619. cifs_put_tlink(tlink);
  620. return rc;
  621. }
  622. struct dentry *
  623. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
  624. unsigned int flags)
  625. {
  626. unsigned int xid;
  627. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  628. struct cifs_sb_info *cifs_sb;
  629. struct tcon_link *tlink;
  630. struct cifs_tcon *pTcon;
  631. struct inode *newInode = NULL;
  632. char *full_path = NULL;
  633. xid = get_xid();
  634. cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
  635. parent_dir_inode, direntry, direntry);
  636. /* check whether path exists */
  637. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  638. tlink = cifs_sb_tlink(cifs_sb);
  639. if (IS_ERR(tlink)) {
  640. free_xid(xid);
  641. return (struct dentry *)tlink;
  642. }
  643. pTcon = tlink_tcon(tlink);
  644. rc = check_name(direntry);
  645. if (rc)
  646. goto lookup_out;
  647. /* can not grab the rename sem here since it would
  648. deadlock in the cases (beginning of sys_rename itself)
  649. in which we already have the sb rename sem */
  650. full_path = build_path_from_dentry(direntry);
  651. if (full_path == NULL) {
  652. rc = -ENOMEM;
  653. goto lookup_out;
  654. }
  655. if (d_really_is_positive(direntry)) {
  656. cifs_dbg(FYI, "non-NULL inode in lookup\n");
  657. } else {
  658. cifs_dbg(FYI, "NULL inode in lookup\n");
  659. }
  660. cifs_dbg(FYI, "Full path: %s inode = 0x%p\n",
  661. full_path, d_inode(direntry));
  662. if (pTcon->unix_ext) {
  663. rc = cifs_get_inode_info_unix(&newInode, full_path,
  664. parent_dir_inode->i_sb, xid);
  665. } else {
  666. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  667. parent_dir_inode->i_sb, xid, NULL);
  668. }
  669. if ((rc == 0) && (newInode != NULL)) {
  670. d_add(direntry, newInode);
  671. /* since paths are not looked up by component - the parent
  672. directories are presumed to be good here */
  673. renew_parental_timestamps(direntry);
  674. } else if (rc == -ENOENT) {
  675. rc = 0;
  676. direntry->d_time = jiffies;
  677. d_add(direntry, NULL);
  678. /* if it was once a directory (but how can we tell?) we could do
  679. shrink_dcache_parent(direntry); */
  680. } else if (rc != -EACCES) {
  681. cifs_dbg(FYI, "Unexpected lookup error %d\n", rc);
  682. /* We special case check for Access Denied - since that
  683. is a common return code */
  684. }
  685. lookup_out:
  686. kfree(full_path);
  687. cifs_put_tlink(tlink);
  688. free_xid(xid);
  689. return ERR_PTR(rc);
  690. }
  691. static int
  692. cifs_d_revalidate(struct dentry *direntry, unsigned int flags)
  693. {
  694. if (flags & LOOKUP_RCU)
  695. return -ECHILD;
  696. if (d_really_is_positive(direntry)) {
  697. if (cifs_revalidate_dentry(direntry))
  698. return 0;
  699. else {
  700. /*
  701. * If the inode wasn't known to be a dfs entry when
  702. * the dentry was instantiated, such as when created
  703. * via ->readdir(), it needs to be set now since the
  704. * attributes will have been updated by
  705. * cifs_revalidate_dentry().
  706. */
  707. if (IS_AUTOMOUNT(d_inode(direntry)) &&
  708. !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
  709. spin_lock(&direntry->d_lock);
  710. direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
  711. spin_unlock(&direntry->d_lock);
  712. }
  713. return 1;
  714. }
  715. }
  716. /*
  717. * This may be nfsd (or something), anyway, we can't see the
  718. * intent of this. So, since this can be for creation, drop it.
  719. */
  720. if (!flags)
  721. return 0;
  722. /*
  723. * Drop the negative dentry, in order to make sure to use the
  724. * case sensitive name which is specified by user if this is
  725. * for creation.
  726. */
  727. if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
  728. return 0;
  729. if (time_after(jiffies, direntry->d_time + HZ) || !lookupCacheEnabled)
  730. return 0;
  731. return 1;
  732. }
  733. /* static int cifs_d_delete(struct dentry *direntry)
  734. {
  735. int rc = 0;
  736. cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry);
  737. return rc;
  738. } */
  739. const struct dentry_operations cifs_dentry_ops = {
  740. .d_revalidate = cifs_d_revalidate,
  741. .d_automount = cifs_dfs_d_automount,
  742. /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
  743. };
  744. static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
  745. {
  746. struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
  747. unsigned long hash;
  748. wchar_t c;
  749. int i, charlen;
  750. hash = init_name_hash();
  751. for (i = 0; i < q->len; i += charlen) {
  752. charlen = codepage->char2uni(&q->name[i], q->len - i, &c);
  753. /* error out if we can't convert the character */
  754. if (unlikely(charlen < 0))
  755. return charlen;
  756. hash = partial_name_hash(cifs_toupper(c), hash);
  757. }
  758. q->hash = end_name_hash(hash);
  759. return 0;
  760. }
  761. static int cifs_ci_compare(const struct dentry *parent, const struct dentry *dentry,
  762. unsigned int len, const char *str, const struct qstr *name)
  763. {
  764. struct nls_table *codepage = CIFS_SB(parent->d_sb)->local_nls;
  765. wchar_t c1, c2;
  766. int i, l1, l2;
  767. /*
  768. * We make the assumption here that uppercase characters in the local
  769. * codepage are always the same length as their lowercase counterparts.
  770. *
  771. * If that's ever not the case, then this will fail to match it.
  772. */
  773. if (name->len != len)
  774. return 1;
  775. for (i = 0; i < len; i += l1) {
  776. /* Convert characters in both strings to UTF-16. */
  777. l1 = codepage->char2uni(&str[i], len - i, &c1);
  778. l2 = codepage->char2uni(&name->name[i], name->len - i, &c2);
  779. /*
  780. * If we can't convert either character, just declare it to
  781. * be 1 byte long and compare the original byte.
  782. */
  783. if (unlikely(l1 < 0 && l2 < 0)) {
  784. if (str[i] != name->name[i])
  785. return 1;
  786. l1 = 1;
  787. continue;
  788. }
  789. /*
  790. * Here, we again ass|u|me that upper/lowercase versions of
  791. * a character are the same length in the local NLS.
  792. */
  793. if (l1 != l2)
  794. return 1;
  795. /* Now compare uppercase versions of these characters */
  796. if (cifs_toupper(c1) != cifs_toupper(c2))
  797. return 1;
  798. }
  799. return 0;
  800. }
  801. const struct dentry_operations cifs_ci_dentry_ops = {
  802. .d_revalidate = cifs_d_revalidate,
  803. .d_hash = cifs_ci_hash,
  804. .d_compare = cifs_ci_compare,
  805. .d_automount = cifs_dfs_d_automount,
  806. };