dir.c 23 KB

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