dir.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. static void
  36. renew_parental_timestamps(struct dentry *direntry)
  37. {
  38. /* BB check if there is a way to get the kernel to do this or if we
  39. really need this */
  40. do {
  41. direntry->d_time = jiffies;
  42. direntry = direntry->d_parent;
  43. } while (!IS_ROOT(direntry));
  44. }
  45. /* Note: caller must free return buffer */
  46. char *
  47. build_path_from_dentry(struct dentry *direntry)
  48. {
  49. struct dentry *temp;
  50. int namelen;
  51. int pplen;
  52. int dfsplen;
  53. char *full_path;
  54. char dirsep;
  55. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  56. struct cifsTconInfo *tcon = cifs_sb_master_tcon(cifs_sb);
  57. if (direntry == NULL)
  58. return NULL; /* not much we can do if dentry is freed and
  59. we need to reopen the file after it was closed implicitly
  60. when the server crashed */
  61. dirsep = CIFS_DIR_SEP(cifs_sb);
  62. pplen = cifs_sb->prepathlen;
  63. if (tcon->Flags & SMB_SHARE_IS_IN_DFS)
  64. dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
  65. else
  66. dfsplen = 0;
  67. cifs_bp_rename_retry:
  68. namelen = pplen + dfsplen;
  69. for (temp = direntry; !IS_ROOT(temp);) {
  70. namelen += (1 + temp->d_name.len);
  71. temp = temp->d_parent;
  72. if (temp == NULL) {
  73. cERROR(1, "corrupt dentry");
  74. return NULL;
  75. }
  76. }
  77. full_path = kmalloc(namelen+1, GFP_KERNEL);
  78. if (full_path == NULL)
  79. return full_path;
  80. full_path[namelen] = 0; /* trailing null */
  81. for (temp = direntry; !IS_ROOT(temp);) {
  82. namelen -= 1 + temp->d_name.len;
  83. if (namelen < 0) {
  84. break;
  85. } else {
  86. full_path[namelen] = dirsep;
  87. strncpy(full_path + namelen + 1, temp->d_name.name,
  88. temp->d_name.len);
  89. cFYI(0, "name: %s", full_path + namelen);
  90. }
  91. temp = temp->d_parent;
  92. if (temp == NULL) {
  93. cERROR(1, "corrupt dentry");
  94. kfree(full_path);
  95. return NULL;
  96. }
  97. }
  98. if (namelen != pplen + dfsplen) {
  99. cERROR(1, "did not end path lookup where expected namelen is %d",
  100. namelen);
  101. /* presumably this is only possible if racing with a rename
  102. of one of the parent directories (we can not lock the dentries
  103. above us to prevent this, but retrying should be harmless) */
  104. kfree(full_path);
  105. goto cifs_bp_rename_retry;
  106. }
  107. /* DIR_SEP already set for byte 0 / vs \ but not for
  108. subsequent slashes in prepath which currently must
  109. be entered the right way - not sure if there is an alternative
  110. since the '\' is a valid posix character so we can not switch
  111. those safely to '/' if any are found in the middle of the prepath */
  112. /* BB test paths to Windows with '/' in the midst of prepath */
  113. if (dfsplen) {
  114. strncpy(full_path, tcon->treeName, dfsplen);
  115. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
  116. int i;
  117. for (i = 0; i < dfsplen; i++) {
  118. if (full_path[i] == '\\')
  119. full_path[i] = '/';
  120. }
  121. }
  122. }
  123. strncpy(full_path + dfsplen, CIFS_SB(direntry->d_sb)->prepath, pplen);
  124. return full_path;
  125. }
  126. struct cifsFileInfo *
  127. cifs_new_fileinfo(struct inode *newinode, __u16 fileHandle, struct file *file,
  128. struct vfsmount *mnt, struct tcon_link *tlink,
  129. unsigned int oflags, __u32 oplock)
  130. {
  131. struct cifsFileInfo *pCifsFile;
  132. struct cifsInodeInfo *pCifsInode;
  133. pCifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  134. if (pCifsFile == NULL)
  135. return pCifsFile;
  136. pCifsFile->netfid = fileHandle;
  137. pCifsFile->pid = current->tgid;
  138. pCifsFile->uid = current_fsuid();
  139. pCifsFile->pInode = igrab(newinode);
  140. pCifsFile->mnt = mnt;
  141. pCifsFile->pfile = file;
  142. pCifsFile->invalidHandle = false;
  143. pCifsFile->closePend = false;
  144. pCifsFile->tlink = cifs_get_tlink(tlink);
  145. mutex_init(&pCifsFile->fh_mutex);
  146. mutex_init(&pCifsFile->lock_mutex);
  147. INIT_LIST_HEAD(&pCifsFile->llist);
  148. atomic_set(&pCifsFile->count, 1);
  149. INIT_WORK(&pCifsFile->oplock_break, cifs_oplock_break);
  150. write_lock(&GlobalSMBSeslock);
  151. list_add(&pCifsFile->tlist, &(tlink_tcon(tlink)->openFileList));
  152. pCifsInode = CIFS_I(newinode);
  153. if (pCifsInode) {
  154. /* if readable file instance put first in list*/
  155. if (oflags & FMODE_READ)
  156. list_add(&pCifsFile->flist, &pCifsInode->openFileList);
  157. else
  158. list_add_tail(&pCifsFile->flist,
  159. &pCifsInode->openFileList);
  160. if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  161. pCifsInode->clientCanCacheAll = true;
  162. pCifsInode->clientCanCacheRead = true;
  163. cFYI(1, "Exclusive Oplock inode %p", newinode);
  164. } else if ((oplock & 0xF) == OPLOCK_READ)
  165. pCifsInode->clientCanCacheRead = true;
  166. }
  167. write_unlock(&GlobalSMBSeslock);
  168. file->private_data = pCifsFile;
  169. return pCifsFile;
  170. }
  171. int cifs_posix_open(char *full_path, struct inode **pinode,
  172. struct super_block *sb, int mode, int oflags,
  173. __u32 *poplock, __u16 *pnetfid, int xid)
  174. {
  175. int rc;
  176. FILE_UNIX_BASIC_INFO *presp_data;
  177. __u32 posix_flags = 0;
  178. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  179. struct cifs_fattr fattr;
  180. struct tcon_link *tlink;
  181. struct cifsTconInfo *tcon;
  182. cFYI(1, "posix open %s", full_path);
  183. presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  184. if (presp_data == NULL)
  185. return -ENOMEM;
  186. /* So far cifs posix extensions can only map the following flags.
  187. There are other valid fmode oflags such as FMODE_LSEEK, FMODE_PREAD, but
  188. so far we do not seem to need them, and we can treat them as local only */
  189. if ((oflags & (FMODE_READ | FMODE_WRITE)) ==
  190. (FMODE_READ | FMODE_WRITE))
  191. posix_flags = SMB_O_RDWR;
  192. else if (oflags & FMODE_READ)
  193. posix_flags = SMB_O_RDONLY;
  194. else if (oflags & FMODE_WRITE)
  195. posix_flags = SMB_O_WRONLY;
  196. if (oflags & O_CREAT)
  197. posix_flags |= SMB_O_CREAT;
  198. if (oflags & O_EXCL)
  199. posix_flags |= SMB_O_EXCL;
  200. if (oflags & O_TRUNC)
  201. posix_flags |= SMB_O_TRUNC;
  202. /* be safe and imply O_SYNC for O_DSYNC */
  203. if (oflags & O_DSYNC)
  204. posix_flags |= SMB_O_SYNC;
  205. if (oflags & O_DIRECTORY)
  206. posix_flags |= SMB_O_DIRECTORY;
  207. if (oflags & O_NOFOLLOW)
  208. posix_flags |= SMB_O_NOFOLLOW;
  209. if (oflags & O_DIRECT)
  210. posix_flags |= SMB_O_DIRECT;
  211. mode &= ~current_umask();
  212. tlink = cifs_sb_tlink(cifs_sb);
  213. if (IS_ERR(tlink)) {
  214. rc = PTR_ERR(tlink);
  215. goto posix_open_ret;
  216. }
  217. tcon = tlink_tcon(tlink);
  218. rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data,
  219. poplock, full_path, cifs_sb->local_nls,
  220. cifs_sb->mnt_cifs_flags &
  221. CIFS_MOUNT_MAP_SPECIAL_CHR);
  222. cifs_put_tlink(tlink);
  223. if (rc)
  224. goto posix_open_ret;
  225. if (presp_data->Type == cpu_to_le32(-1))
  226. goto posix_open_ret; /* open ok, caller does qpathinfo */
  227. if (!pinode)
  228. goto posix_open_ret; /* caller does not need info */
  229. cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb);
  230. /* get new inode and set it up */
  231. if (*pinode == NULL) {
  232. cifs_fill_uniqueid(sb, &fattr);
  233. *pinode = cifs_iget(sb, &fattr);
  234. if (!*pinode) {
  235. rc = -ENOMEM;
  236. goto posix_open_ret;
  237. }
  238. } else {
  239. cifs_fattr_to_inode(*pinode, &fattr);
  240. }
  241. posix_open_ret:
  242. kfree(presp_data);
  243. return rc;
  244. }
  245. static void setup_cifs_dentry(struct cifsTconInfo *tcon,
  246. struct dentry *direntry,
  247. struct inode *newinode)
  248. {
  249. if (tcon->nocase)
  250. direntry->d_op = &cifs_ci_dentry_ops;
  251. else
  252. direntry->d_op = &cifs_dentry_ops;
  253. d_instantiate(direntry, newinode);
  254. }
  255. /* Inode operations in similar order to how they appear in Linux file fs.h */
  256. int
  257. cifs_create(struct inode *inode, struct dentry *direntry, int mode,
  258. struct nameidata *nd)
  259. {
  260. int rc = -ENOENT;
  261. int xid;
  262. int create_options = CREATE_NOT_DIR;
  263. __u32 oplock = 0;
  264. int oflags;
  265. /*
  266. * BB below access is probably too much for mknod to request
  267. * but we have to do query and setpathinfo so requesting
  268. * less could fail (unless we want to request getatr and setatr
  269. * permissions (only). At least for POSIX we do not have to
  270. * request so much.
  271. */
  272. int desiredAccess = GENERIC_READ | GENERIC_WRITE;
  273. __u16 fileHandle;
  274. struct cifs_sb_info *cifs_sb;
  275. struct tcon_link *tlink;
  276. struct cifsTconInfo *tcon;
  277. char *full_path = NULL;
  278. FILE_ALL_INFO *buf = NULL;
  279. struct inode *newinode = NULL;
  280. int disposition = FILE_OVERWRITE_IF;
  281. xid = GetXid();
  282. cifs_sb = CIFS_SB(inode->i_sb);
  283. tlink = cifs_sb_tlink(cifs_sb);
  284. if (IS_ERR(tlink)) {
  285. FreeXid(xid);
  286. return PTR_ERR(tlink);
  287. }
  288. tcon = tlink_tcon(tlink);
  289. if (oplockEnabled)
  290. oplock = REQ_OPLOCK;
  291. if (nd && (nd->flags & LOOKUP_OPEN))
  292. oflags = nd->intent.open.flags;
  293. else
  294. oflags = FMODE_READ | SMB_O_CREAT;
  295. full_path = build_path_from_dentry(direntry);
  296. if (full_path == NULL) {
  297. rc = -ENOMEM;
  298. goto cifs_create_out;
  299. }
  300. if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) &&
  301. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  302. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  303. rc = cifs_posix_open(full_path, &newinode,
  304. inode->i_sb, mode, oflags, &oplock, &fileHandle, xid);
  305. /* EIO could indicate that (posix open) operation is not
  306. supported, despite what server claimed in capability
  307. negotation. EREMOTE indicates DFS junction, which is not
  308. handled in posix open */
  309. if (rc == 0) {
  310. if (newinode == NULL) /* query inode info */
  311. goto cifs_create_get_file_info;
  312. else /* success, no need to query */
  313. goto cifs_create_set_dentry;
  314. } else if ((rc != -EIO) && (rc != -EREMOTE) &&
  315. (rc != -EOPNOTSUPP) && (rc != -EINVAL))
  316. goto cifs_create_out;
  317. /* else fallthrough to retry, using older open call, this is
  318. case where server does not support this SMB level, and
  319. falsely claims capability (also get here for DFS case
  320. which should be rare for path not covered on files) */
  321. }
  322. if (nd && (nd->flags & LOOKUP_OPEN)) {
  323. /* if the file is going to stay open, then we
  324. need to set the desired access properly */
  325. desiredAccess = 0;
  326. if (oflags & FMODE_READ)
  327. desiredAccess |= GENERIC_READ; /* is this too little? */
  328. if (oflags & FMODE_WRITE)
  329. desiredAccess |= GENERIC_WRITE;
  330. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  331. disposition = FILE_CREATE;
  332. else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  333. disposition = FILE_OVERWRITE_IF;
  334. else if ((oflags & O_CREAT) == O_CREAT)
  335. disposition = FILE_OPEN_IF;
  336. else
  337. cFYI(1, "Create flag not set in create function");
  338. }
  339. /* BB add processing to set equivalent of mode - e.g. via CreateX with
  340. ACLs */
  341. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  342. if (buf == NULL) {
  343. rc = -ENOMEM;
  344. goto cifs_create_out;
  345. }
  346. /*
  347. * if we're not using unix extensions, see if we need to set
  348. * ATTR_READONLY on the create call
  349. */
  350. if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
  351. create_options |= CREATE_OPTION_READONLY;
  352. if (tcon->ses->capabilities & CAP_NT_SMBS)
  353. rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
  354. desiredAccess, create_options,
  355. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  356. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  357. else
  358. rc = -EIO; /* no NT SMB support fall into legacy open below */
  359. if (rc == -EIO) {
  360. /* old server, retry the open legacy style */
  361. rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
  362. desiredAccess, create_options,
  363. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  364. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  365. }
  366. if (rc) {
  367. cFYI(1, "cifs_create returned 0x%x", rc);
  368. goto cifs_create_out;
  369. }
  370. /* If Open reported that we actually created a file
  371. then we now have to set the mode if possible */
  372. if ((tcon->unix_ext) && (oplock & CIFS_CREATE_ACTION)) {
  373. struct cifs_unix_set_info_args args = {
  374. .mode = mode,
  375. .ctime = NO_CHANGE_64,
  376. .atime = NO_CHANGE_64,
  377. .mtime = NO_CHANGE_64,
  378. .device = 0,
  379. };
  380. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  381. args.uid = (__u64) current_fsuid();
  382. if (inode->i_mode & S_ISGID)
  383. args.gid = (__u64) inode->i_gid;
  384. else
  385. args.gid = (__u64) current_fsgid();
  386. } else {
  387. args.uid = NO_CHANGE_64;
  388. args.gid = NO_CHANGE_64;
  389. }
  390. CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
  391. cifs_sb->local_nls,
  392. cifs_sb->mnt_cifs_flags &
  393. CIFS_MOUNT_MAP_SPECIAL_CHR);
  394. } else {
  395. /* BB implement mode setting via Windows security
  396. descriptors e.g. */
  397. /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
  398. /* Could set r/o dos attribute if mode & 0222 == 0 */
  399. }
  400. cifs_create_get_file_info:
  401. /* server might mask mode so we have to query for it */
  402. if (tcon->unix_ext)
  403. rc = cifs_get_inode_info_unix(&newinode, full_path,
  404. inode->i_sb, xid);
  405. else {
  406. rc = cifs_get_inode_info(&newinode, full_path, buf,
  407. inode->i_sb, xid, &fileHandle);
  408. if (newinode) {
  409. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  410. newinode->i_mode = mode;
  411. if ((oplock & CIFS_CREATE_ACTION) &&
  412. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) {
  413. newinode->i_uid = current_fsuid();
  414. if (inode->i_mode & S_ISGID)
  415. newinode->i_gid = inode->i_gid;
  416. else
  417. newinode->i_gid = current_fsgid();
  418. }
  419. }
  420. }
  421. cifs_create_set_dentry:
  422. if (rc == 0)
  423. setup_cifs_dentry(tcon, direntry, newinode);
  424. else
  425. cFYI(1, "Create worked, get_inode_info failed rc = %d", rc);
  426. if (newinode && nd && (nd->flags & LOOKUP_OPEN)) {
  427. struct cifsFileInfo *pfile_info;
  428. struct file *filp;
  429. filp = lookup_instantiate_filp(nd, direntry, generic_file_open);
  430. if (IS_ERR(filp)) {
  431. rc = PTR_ERR(filp);
  432. CIFSSMBClose(xid, tcon, fileHandle);
  433. goto cifs_create_out;
  434. }
  435. pfile_info = cifs_new_fileinfo(newinode, fileHandle, filp,
  436. nd->path.mnt, tlink, oflags,
  437. oplock);
  438. if (pfile_info == NULL) {
  439. fput(filp);
  440. CIFSSMBClose(xid, tcon, fileHandle);
  441. rc = -ENOMEM;
  442. }
  443. } else {
  444. CIFSSMBClose(xid, tcon, fileHandle);
  445. }
  446. cifs_create_out:
  447. kfree(buf);
  448. kfree(full_path);
  449. cifs_put_tlink(tlink);
  450. FreeXid(xid);
  451. return rc;
  452. }
  453. int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode,
  454. dev_t device_number)
  455. {
  456. int rc = -EPERM;
  457. int xid;
  458. struct cifs_sb_info *cifs_sb;
  459. struct tcon_link *tlink;
  460. struct cifsTconInfo *pTcon;
  461. char *full_path = NULL;
  462. struct inode *newinode = NULL;
  463. int oplock = 0;
  464. u16 fileHandle;
  465. FILE_ALL_INFO *buf = NULL;
  466. unsigned int bytes_written;
  467. struct win_dev *pdev;
  468. if (!old_valid_dev(device_number))
  469. return -EINVAL;
  470. cifs_sb = CIFS_SB(inode->i_sb);
  471. tlink = cifs_sb_tlink(cifs_sb);
  472. if (IS_ERR(tlink))
  473. return PTR_ERR(tlink);
  474. pTcon = tlink_tcon(tlink);
  475. xid = GetXid();
  476. full_path = build_path_from_dentry(direntry);
  477. if (full_path == NULL) {
  478. rc = -ENOMEM;
  479. goto mknod_out;
  480. }
  481. if (pTcon->unix_ext) {
  482. struct cifs_unix_set_info_args args = {
  483. .mode = mode & ~current_umask(),
  484. .ctime = NO_CHANGE_64,
  485. .atime = NO_CHANGE_64,
  486. .mtime = NO_CHANGE_64,
  487. .device = device_number,
  488. };
  489. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  490. args.uid = (__u64) current_fsuid();
  491. args.gid = (__u64) current_fsgid();
  492. } else {
  493. args.uid = NO_CHANGE_64;
  494. args.gid = NO_CHANGE_64;
  495. }
  496. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args,
  497. cifs_sb->local_nls,
  498. cifs_sb->mnt_cifs_flags &
  499. CIFS_MOUNT_MAP_SPECIAL_CHR);
  500. if (rc)
  501. goto mknod_out;
  502. rc = cifs_get_inode_info_unix(&newinode, full_path,
  503. inode->i_sb, xid);
  504. if (pTcon->nocase)
  505. direntry->d_op = &cifs_ci_dentry_ops;
  506. else
  507. direntry->d_op = &cifs_dentry_ops;
  508. if (rc == 0)
  509. d_instantiate(direntry, newinode);
  510. goto mknod_out;
  511. }
  512. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
  513. goto mknod_out;
  514. cFYI(1, "sfu compat create special file");
  515. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  516. if (buf == NULL) {
  517. kfree(full_path);
  518. rc = -ENOMEM;
  519. FreeXid(xid);
  520. return rc;
  521. }
  522. /* FIXME: would WRITE_OWNER | WRITE_DAC be better? */
  523. rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_CREATE,
  524. GENERIC_WRITE, CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
  525. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  526. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  527. if (rc)
  528. goto mknod_out;
  529. /* BB Do not bother to decode buf since no local inode yet to put
  530. * timestamps in, but we can reuse it safely */
  531. pdev = (struct win_dev *)buf;
  532. if (S_ISCHR(mode)) {
  533. memcpy(pdev->type, "IntxCHR", 8);
  534. pdev->major =
  535. cpu_to_le64(MAJOR(device_number));
  536. pdev->minor =
  537. cpu_to_le64(MINOR(device_number));
  538. rc = CIFSSMBWrite(xid, pTcon,
  539. fileHandle,
  540. sizeof(struct win_dev),
  541. 0, &bytes_written, (char *)pdev,
  542. NULL, 0);
  543. } else if (S_ISBLK(mode)) {
  544. memcpy(pdev->type, "IntxBLK", 8);
  545. pdev->major =
  546. cpu_to_le64(MAJOR(device_number));
  547. pdev->minor =
  548. cpu_to_le64(MINOR(device_number));
  549. rc = CIFSSMBWrite(xid, pTcon,
  550. fileHandle,
  551. sizeof(struct win_dev),
  552. 0, &bytes_written, (char *)pdev,
  553. NULL, 0);
  554. } /* else if (S_ISFIFO) */
  555. CIFSSMBClose(xid, pTcon, fileHandle);
  556. d_drop(direntry);
  557. /* FIXME: add code here to set EAs */
  558. mknod_out:
  559. kfree(full_path);
  560. kfree(buf);
  561. FreeXid(xid);
  562. cifs_put_tlink(tlink);
  563. return rc;
  564. }
  565. struct dentry *
  566. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
  567. struct nameidata *nd)
  568. {
  569. int xid;
  570. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  571. __u32 oplock = 0;
  572. __u16 fileHandle = 0;
  573. bool posix_open = false;
  574. struct cifs_sb_info *cifs_sb;
  575. struct tcon_link *tlink;
  576. struct cifsTconInfo *pTcon;
  577. struct cifsFileInfo *cfile;
  578. struct inode *newInode = NULL;
  579. char *full_path = NULL;
  580. struct file *filp;
  581. xid = GetXid();
  582. cFYI(1, "parent inode = 0x%p name is: %s and dentry = 0x%p",
  583. parent_dir_inode, direntry->d_name.name, direntry);
  584. /* check whether path exists */
  585. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  586. tlink = cifs_sb_tlink(cifs_sb);
  587. if (IS_ERR(tlink)) {
  588. FreeXid(xid);
  589. return (struct dentry *)tlink;
  590. }
  591. pTcon = tlink_tcon(tlink);
  592. /*
  593. * Don't allow the separator character in a path component.
  594. * The VFS will not allow "/", but "\" is allowed by posix.
  595. */
  596. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
  597. int i;
  598. for (i = 0; i < direntry->d_name.len; i++)
  599. if (direntry->d_name.name[i] == '\\') {
  600. cFYI(1, "Invalid file name");
  601. rc = -EINVAL;
  602. goto lookup_out;
  603. }
  604. }
  605. /*
  606. * O_EXCL: optimize away the lookup, but don't hash the dentry. Let
  607. * the VFS handle the create.
  608. */
  609. if (nd && (nd->flags & LOOKUP_EXCL)) {
  610. d_instantiate(direntry, NULL);
  611. rc = 0;
  612. goto lookup_out;
  613. }
  614. /* can not grab the rename sem here since it would
  615. deadlock in the cases (beginning of sys_rename itself)
  616. in which we already have the sb rename sem */
  617. full_path = build_path_from_dentry(direntry);
  618. if (full_path == NULL) {
  619. rc = -ENOMEM;
  620. goto lookup_out;
  621. }
  622. if (direntry->d_inode != NULL) {
  623. cFYI(1, "non-NULL inode in lookup");
  624. } else {
  625. cFYI(1, "NULL inode in lookup");
  626. }
  627. cFYI(1, "Full path: %s inode = 0x%p", full_path, direntry->d_inode);
  628. /* Posix open is only called (at lookup time) for file create now.
  629. * For opens (rather than creates), because we do not know if it
  630. * is a file or directory yet, and current Samba no longer allows
  631. * us to do posix open on dirs, we could end up wasting an open call
  632. * on what turns out to be a dir. For file opens, we wait to call posix
  633. * open till cifs_open. It could be added here (lookup) in the future
  634. * but the performance tradeoff of the extra network request when EISDIR
  635. * or EACCES is returned would have to be weighed against the 50%
  636. * reduction in network traffic in the other paths.
  637. */
  638. if (pTcon->unix_ext) {
  639. if (nd && !(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY)) &&
  640. (nd->flags & LOOKUP_OPEN) && !pTcon->broken_posix_open &&
  641. (nd->intent.open.flags & O_CREAT)) {
  642. rc = cifs_posix_open(full_path, &newInode,
  643. parent_dir_inode->i_sb,
  644. nd->intent.open.create_mode,
  645. nd->intent.open.flags, &oplock,
  646. &fileHandle, xid);
  647. /*
  648. * The check below works around a bug in POSIX
  649. * open in samba versions 3.3.1 and earlier where
  650. * open could incorrectly fail with invalid parameter.
  651. * If either that or op not supported returned, follow
  652. * the normal lookup.
  653. */
  654. if ((rc == 0) || (rc == -ENOENT))
  655. posix_open = true;
  656. else if ((rc == -EINVAL) || (rc != -EOPNOTSUPP))
  657. pTcon->broken_posix_open = true;
  658. }
  659. if (!posix_open)
  660. rc = cifs_get_inode_info_unix(&newInode, full_path,
  661. parent_dir_inode->i_sb, xid);
  662. } else
  663. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  664. parent_dir_inode->i_sb, xid, NULL);
  665. if ((rc == 0) && (newInode != NULL)) {
  666. if (pTcon->nocase)
  667. direntry->d_op = &cifs_ci_dentry_ops;
  668. else
  669. direntry->d_op = &cifs_dentry_ops;
  670. d_add(direntry, newInode);
  671. if (posix_open) {
  672. filp = lookup_instantiate_filp(nd, direntry,
  673. generic_file_open);
  674. if (IS_ERR(filp)) {
  675. rc = PTR_ERR(filp);
  676. CIFSSMBClose(xid, pTcon, fileHandle);
  677. goto lookup_out;
  678. }
  679. cfile = cifs_new_fileinfo(newInode, fileHandle, filp,
  680. nd->path.mnt, tlink,
  681. nd->intent.open.flags,
  682. oplock);
  683. if (cfile == NULL) {
  684. fput(filp);
  685. CIFSSMBClose(xid, pTcon, fileHandle);
  686. rc = -ENOMEM;
  687. goto lookup_out;
  688. }
  689. }
  690. /* since paths are not looked up by component - the parent
  691. directories are presumed to be good here */
  692. renew_parental_timestamps(direntry);
  693. } else if (rc == -ENOENT) {
  694. rc = 0;
  695. direntry->d_time = jiffies;
  696. if (pTcon->nocase)
  697. direntry->d_op = &cifs_ci_dentry_ops;
  698. else
  699. direntry->d_op = &cifs_dentry_ops;
  700. d_add(direntry, NULL);
  701. /* if it was once a directory (but how can we tell?) we could do
  702. shrink_dcache_parent(direntry); */
  703. } else if (rc != -EACCES) {
  704. cERROR(1, "Unexpected lookup error %d", rc);
  705. /* We special case check for Access Denied - since that
  706. is a common return code */
  707. }
  708. lookup_out:
  709. kfree(full_path);
  710. cifs_put_tlink(tlink);
  711. FreeXid(xid);
  712. return ERR_PTR(rc);
  713. }
  714. static int
  715. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  716. {
  717. int isValid = 1;
  718. if (direntry->d_inode) {
  719. if (cifs_revalidate_dentry(direntry))
  720. return 0;
  721. } else {
  722. cFYI(1, "neg dentry 0x%p name = %s",
  723. direntry, direntry->d_name.name);
  724. if (time_after(jiffies, direntry->d_time + HZ) ||
  725. !lookupCacheEnabled) {
  726. d_drop(direntry);
  727. isValid = 0;
  728. }
  729. }
  730. return isValid;
  731. }
  732. /* static int cifs_d_delete(struct dentry *direntry)
  733. {
  734. int rc = 0;
  735. cFYI(1, "In cifs d_delete, name = %s", direntry->d_name.name);
  736. return rc;
  737. } */
  738. const struct dentry_operations cifs_dentry_ops = {
  739. .d_revalidate = cifs_d_revalidate,
  740. /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
  741. };
  742. static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
  743. {
  744. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  745. unsigned long hash;
  746. int i;
  747. hash = init_name_hash();
  748. for (i = 0; i < q->len; i++)
  749. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  750. hash);
  751. q->hash = end_name_hash(hash);
  752. return 0;
  753. }
  754. static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
  755. struct qstr *b)
  756. {
  757. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  758. if ((a->len == b->len) &&
  759. (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
  760. /*
  761. * To preserve case, don't let an existing negative dentry's
  762. * case take precedence. If a is not a negative dentry, this
  763. * should have no side effects
  764. */
  765. memcpy((void *)a->name, b->name, a->len);
  766. return 0;
  767. }
  768. return 1;
  769. }
  770. const struct dentry_operations cifs_ci_dentry_ops = {
  771. .d_revalidate = cifs_d_revalidate,
  772. .d_hash = cifs_ci_hash,
  773. .d_compare = cifs_ci_compare,
  774. };