dir.c 26 KB

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