dir.c 25 KB

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