dir.c 25 KB

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