dir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. slow_work_init(&pCifsFile->oplock_break, &cifs_oplock_break_ops);
  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. FreeXid(xid);
  279. return rc;
  280. }
  281. if (oplockEnabled)
  282. oplock = REQ_OPLOCK;
  283. if (nd && (nd->flags & LOOKUP_OPEN))
  284. oflags = nd->intent.open.flags;
  285. else
  286. oflags = FMODE_READ | SMB_O_CREAT;
  287. if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) &&
  288. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  289. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  290. rc = cifs_posix_open(full_path, &newinode,
  291. inode->i_sb, mode, oflags, &oplock, &fileHandle, xid);
  292. /* EIO could indicate that (posix open) operation is not
  293. supported, despite what server claimed in capability
  294. negotation. EREMOTE indicates DFS junction, which is not
  295. handled in posix open */
  296. if (rc == 0) {
  297. if (newinode == NULL) /* query inode info */
  298. goto cifs_create_get_file_info;
  299. else /* success, no need to query */
  300. goto cifs_create_set_dentry;
  301. } else if ((rc != -EIO) && (rc != -EREMOTE) &&
  302. (rc != -EOPNOTSUPP) && (rc != -EINVAL))
  303. goto cifs_create_out;
  304. /* else fallthrough to retry, using older open call, this is
  305. case where server does not support this SMB level, and
  306. falsely claims capability (also get here for DFS case
  307. which should be rare for path not covered on files) */
  308. }
  309. if (nd && (nd->flags & LOOKUP_OPEN)) {
  310. /* if the file is going to stay open, then we
  311. need to set the desired access properly */
  312. desiredAccess = 0;
  313. if (oflags & FMODE_READ)
  314. desiredAccess |= GENERIC_READ; /* is this too little? */
  315. if (oflags & FMODE_WRITE)
  316. desiredAccess |= GENERIC_WRITE;
  317. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  318. disposition = FILE_CREATE;
  319. else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  320. disposition = FILE_OVERWRITE_IF;
  321. else if ((oflags & O_CREAT) == O_CREAT)
  322. disposition = FILE_OPEN_IF;
  323. else
  324. cFYI(1, "Create flag not set in create function");
  325. }
  326. /* BB add processing to set equivalent of mode - e.g. via CreateX with
  327. ACLs */
  328. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  329. if (buf == NULL) {
  330. kfree(full_path);
  331. FreeXid(xid);
  332. return -ENOMEM;
  333. }
  334. /*
  335. * if we're not using unix extensions, see if we need to set
  336. * ATTR_READONLY on the create call
  337. */
  338. if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
  339. create_options |= CREATE_OPTION_READONLY;
  340. if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS)
  341. rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
  342. desiredAccess, create_options,
  343. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  344. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  345. else
  346. rc = -EIO; /* no NT SMB support fall into legacy open below */
  347. if (rc == -EIO) {
  348. /* old server, retry the open legacy style */
  349. rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
  350. desiredAccess, create_options,
  351. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  352. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  353. }
  354. if (rc) {
  355. cFYI(1, "cifs_create returned 0x%x", rc);
  356. goto cifs_create_out;
  357. }
  358. /* If Open reported that we actually created a file
  359. then we now have to set the mode if possible */
  360. if ((tcon->unix_ext) && (oplock & CIFS_CREATE_ACTION)) {
  361. struct cifs_unix_set_info_args args = {
  362. .mode = mode,
  363. .ctime = NO_CHANGE_64,
  364. .atime = NO_CHANGE_64,
  365. .mtime = NO_CHANGE_64,
  366. .device = 0,
  367. };
  368. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  369. args.uid = (__u64) current_fsuid();
  370. if (inode->i_mode & S_ISGID)
  371. args.gid = (__u64) inode->i_gid;
  372. else
  373. args.gid = (__u64) current_fsgid();
  374. } else {
  375. args.uid = NO_CHANGE_64;
  376. args.gid = NO_CHANGE_64;
  377. }
  378. CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
  379. cifs_sb->local_nls,
  380. cifs_sb->mnt_cifs_flags &
  381. CIFS_MOUNT_MAP_SPECIAL_CHR);
  382. } else {
  383. /* BB implement mode setting via Windows security
  384. descriptors e.g. */
  385. /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
  386. /* Could set r/o dos attribute if mode & 0222 == 0 */
  387. }
  388. cifs_create_get_file_info:
  389. /* server might mask mode so we have to query for it */
  390. if (tcon->unix_ext)
  391. rc = cifs_get_inode_info_unix(&newinode, full_path,
  392. inode->i_sb, xid);
  393. else {
  394. rc = cifs_get_inode_info(&newinode, full_path, buf,
  395. inode->i_sb, xid, &fileHandle);
  396. if (newinode) {
  397. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  398. newinode->i_mode = mode;
  399. if ((oplock & CIFS_CREATE_ACTION) &&
  400. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) {
  401. newinode->i_uid = current_fsuid();
  402. if (inode->i_mode & S_ISGID)
  403. newinode->i_gid = inode->i_gid;
  404. else
  405. newinode->i_gid = current_fsgid();
  406. }
  407. }
  408. }
  409. cifs_create_set_dentry:
  410. if (rc == 0)
  411. setup_cifs_dentry(tcon, direntry, newinode);
  412. else
  413. cFYI(1, "Create worked, get_inode_info failed rc = %d", rc);
  414. if (newinode && nd && (nd->flags & LOOKUP_OPEN)) {
  415. struct cifsFileInfo *pfile_info;
  416. struct file *filp;
  417. filp = lookup_instantiate_filp(nd, direntry, generic_file_open);
  418. if (IS_ERR(filp)) {
  419. rc = PTR_ERR(filp);
  420. CIFSSMBClose(xid, tcon, fileHandle);
  421. goto cifs_create_out;
  422. }
  423. pfile_info = cifs_new_fileinfo(newinode, fileHandle, filp,
  424. nd->path.mnt, oflags);
  425. if (pfile_info == NULL) {
  426. fput(filp);
  427. CIFSSMBClose(xid, tcon, fileHandle);
  428. rc = -ENOMEM;
  429. }
  430. } else {
  431. CIFSSMBClose(xid, tcon, fileHandle);
  432. }
  433. cifs_create_out:
  434. kfree(buf);
  435. kfree(full_path);
  436. FreeXid(xid);
  437. return rc;
  438. }
  439. int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode,
  440. dev_t device_number)
  441. {
  442. int rc = -EPERM;
  443. int xid;
  444. struct cifs_sb_info *cifs_sb;
  445. struct cifsTconInfo *pTcon;
  446. char *full_path = NULL;
  447. struct inode *newinode = NULL;
  448. if (!old_valid_dev(device_number))
  449. return -EINVAL;
  450. xid = GetXid();
  451. cifs_sb = CIFS_SB(inode->i_sb);
  452. pTcon = cifs_sb->tcon;
  453. full_path = build_path_from_dentry(direntry);
  454. if (full_path == NULL)
  455. rc = -ENOMEM;
  456. else if (pTcon->unix_ext) {
  457. struct cifs_unix_set_info_args args = {
  458. .mode = mode & ~current_umask(),
  459. .ctime = NO_CHANGE_64,
  460. .atime = NO_CHANGE_64,
  461. .mtime = NO_CHANGE_64,
  462. .device = device_number,
  463. };
  464. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  465. args.uid = (__u64) current_fsuid();
  466. args.gid = (__u64) current_fsgid();
  467. } else {
  468. args.uid = NO_CHANGE_64;
  469. args.gid = NO_CHANGE_64;
  470. }
  471. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args,
  472. cifs_sb->local_nls,
  473. cifs_sb->mnt_cifs_flags &
  474. CIFS_MOUNT_MAP_SPECIAL_CHR);
  475. if (!rc) {
  476. rc = cifs_get_inode_info_unix(&newinode, full_path,
  477. inode->i_sb, xid);
  478. if (pTcon->nocase)
  479. direntry->d_op = &cifs_ci_dentry_ops;
  480. else
  481. direntry->d_op = &cifs_dentry_ops;
  482. if (rc == 0)
  483. d_instantiate(direntry, newinode);
  484. }
  485. } else {
  486. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  487. int oplock = 0;
  488. u16 fileHandle;
  489. FILE_ALL_INFO *buf;
  490. cFYI(1, "sfu compat create special file");
  491. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  492. if (buf == NULL) {
  493. kfree(full_path);
  494. rc = -ENOMEM;
  495. FreeXid(xid);
  496. return rc;
  497. }
  498. rc = CIFSSMBOpen(xid, pTcon, full_path,
  499. FILE_CREATE, /* fail if exists */
  500. GENERIC_WRITE /* BB would
  501. WRITE_OWNER | WRITE_DAC be better? */,
  502. /* Create a file and set the
  503. file attribute to SYSTEM */
  504. CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
  505. &fileHandle, &oplock, buf,
  506. cifs_sb->local_nls,
  507. cifs_sb->mnt_cifs_flags &
  508. CIFS_MOUNT_MAP_SPECIAL_CHR);
  509. /* BB FIXME - add handling for backlevel servers
  510. which need legacy open and check for all
  511. calls to SMBOpen for fallback to SMBLeagcyOpen */
  512. if (!rc) {
  513. /* BB Do not bother to decode buf since no
  514. local inode yet to put timestamps in,
  515. but we can reuse it safely */
  516. unsigned int bytes_written;
  517. struct win_dev *pdev;
  518. pdev = (struct win_dev *)buf;
  519. if (S_ISCHR(mode)) {
  520. memcpy(pdev->type, "IntxCHR", 8);
  521. pdev->major =
  522. cpu_to_le64(MAJOR(device_number));
  523. pdev->minor =
  524. cpu_to_le64(MINOR(device_number));
  525. rc = CIFSSMBWrite(xid, pTcon,
  526. fileHandle,
  527. sizeof(struct win_dev),
  528. 0, &bytes_written, (char *)pdev,
  529. NULL, 0);
  530. } else if (S_ISBLK(mode)) {
  531. memcpy(pdev->type, "IntxBLK", 8);
  532. pdev->major =
  533. cpu_to_le64(MAJOR(device_number));
  534. pdev->minor =
  535. cpu_to_le64(MINOR(device_number));
  536. rc = CIFSSMBWrite(xid, pTcon,
  537. fileHandle,
  538. sizeof(struct win_dev),
  539. 0, &bytes_written, (char *)pdev,
  540. NULL, 0);
  541. } /* else if(S_ISFIFO */
  542. CIFSSMBClose(xid, pTcon, fileHandle);
  543. d_drop(direntry);
  544. }
  545. kfree(buf);
  546. /* add code here to set EAs */
  547. }
  548. }
  549. kfree(full_path);
  550. FreeXid(xid);
  551. return rc;
  552. }
  553. struct dentry *
  554. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
  555. struct nameidata *nd)
  556. {
  557. int xid;
  558. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  559. __u32 oplock = 0;
  560. __u16 fileHandle = 0;
  561. bool posix_open = false;
  562. struct cifs_sb_info *cifs_sb;
  563. struct cifsTconInfo *pTcon;
  564. struct cifsFileInfo *cfile;
  565. struct inode *newInode = NULL;
  566. char *full_path = NULL;
  567. struct file *filp;
  568. xid = GetXid();
  569. cFYI(1, "parent inode = 0x%p name is: %s and dentry = 0x%p",
  570. parent_dir_inode, direntry->d_name.name, direntry);
  571. /* check whether path exists */
  572. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  573. pTcon = cifs_sb->tcon;
  574. /*
  575. * Don't allow the separator character in a path component.
  576. * The VFS will not allow "/", but "\" is allowed by posix.
  577. */
  578. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
  579. int i;
  580. for (i = 0; i < direntry->d_name.len; i++)
  581. if (direntry->d_name.name[i] == '\\') {
  582. cFYI(1, "Invalid file name");
  583. FreeXid(xid);
  584. return ERR_PTR(-EINVAL);
  585. }
  586. }
  587. /*
  588. * O_EXCL: optimize away the lookup, but don't hash the dentry. Let
  589. * the VFS handle the create.
  590. */
  591. if (nd && (nd->flags & LOOKUP_EXCL)) {
  592. d_instantiate(direntry, NULL);
  593. return NULL;
  594. }
  595. /* can not grab the rename sem here since it would
  596. deadlock in the cases (beginning of sys_rename itself)
  597. in which we already have the sb rename sem */
  598. full_path = build_path_from_dentry(direntry);
  599. if (full_path == NULL) {
  600. FreeXid(xid);
  601. return ERR_PTR(-ENOMEM);
  602. }
  603. if (direntry->d_inode != NULL) {
  604. cFYI(1, "non-NULL inode in lookup");
  605. } else {
  606. cFYI(1, "NULL inode in lookup");
  607. }
  608. cFYI(1, "Full path: %s inode = 0x%p", full_path, direntry->d_inode);
  609. /* Posix open is only called (at lookup time) for file create now.
  610. * For opens (rather than creates), because we do not know if it
  611. * is a file or directory yet, and current Samba no longer allows
  612. * us to do posix open on dirs, we could end up wasting an open call
  613. * on what turns out to be a dir. For file opens, we wait to call posix
  614. * open till cifs_open. It could be added here (lookup) in the future
  615. * but the performance tradeoff of the extra network request when EISDIR
  616. * or EACCES is returned would have to be weighed against the 50%
  617. * reduction in network traffic in the other paths.
  618. */
  619. if (pTcon->unix_ext) {
  620. if (nd && !(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY)) &&
  621. (nd->flags & LOOKUP_OPEN) && !pTcon->broken_posix_open &&
  622. (nd->intent.open.flags & O_CREAT)) {
  623. rc = cifs_posix_open(full_path, &newInode,
  624. parent_dir_inode->i_sb,
  625. nd->intent.open.create_mode,
  626. nd->intent.open.flags, &oplock,
  627. &fileHandle, xid);
  628. /*
  629. * The check below works around a bug in POSIX
  630. * open in samba versions 3.3.1 and earlier where
  631. * open could incorrectly fail with invalid parameter.
  632. * If either that or op not supported returned, follow
  633. * the normal lookup.
  634. */
  635. if ((rc == 0) || (rc == -ENOENT))
  636. posix_open = true;
  637. else if ((rc == -EINVAL) || (rc != -EOPNOTSUPP))
  638. pTcon->broken_posix_open = true;
  639. }
  640. if (!posix_open)
  641. rc = cifs_get_inode_info_unix(&newInode, full_path,
  642. parent_dir_inode->i_sb, xid);
  643. } else
  644. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  645. parent_dir_inode->i_sb, xid, NULL);
  646. if ((rc == 0) && (newInode != NULL)) {
  647. if (pTcon->nocase)
  648. direntry->d_op = &cifs_ci_dentry_ops;
  649. else
  650. direntry->d_op = &cifs_dentry_ops;
  651. d_add(direntry, newInode);
  652. if (posix_open) {
  653. filp = lookup_instantiate_filp(nd, direntry,
  654. generic_file_open);
  655. if (IS_ERR(filp)) {
  656. rc = PTR_ERR(filp);
  657. CIFSSMBClose(xid, pTcon, fileHandle);
  658. goto lookup_out;
  659. }
  660. cfile = cifs_new_fileinfo(newInode, fileHandle, filp,
  661. nd->path.mnt,
  662. nd->intent.open.flags);
  663. if (cfile == NULL) {
  664. fput(filp);
  665. CIFSSMBClose(xid, pTcon, fileHandle);
  666. rc = -ENOMEM;
  667. goto lookup_out;
  668. }
  669. }
  670. /* since paths are not looked up by component - the parent
  671. directories are presumed to be good here */
  672. renew_parental_timestamps(direntry);
  673. } else if (rc == -ENOENT) {
  674. rc = 0;
  675. direntry->d_time = jiffies;
  676. if (pTcon->nocase)
  677. direntry->d_op = &cifs_ci_dentry_ops;
  678. else
  679. direntry->d_op = &cifs_dentry_ops;
  680. d_add(direntry, NULL);
  681. /* if it was once a directory (but how can we tell?) we could do
  682. shrink_dcache_parent(direntry); */
  683. } else if (rc != -EACCES) {
  684. cERROR(1, "Unexpected lookup error %d", rc);
  685. /* We special case check for Access Denied - since that
  686. is a common return code */
  687. }
  688. lookup_out:
  689. kfree(full_path);
  690. FreeXid(xid);
  691. return ERR_PTR(rc);
  692. }
  693. static int
  694. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  695. {
  696. int isValid = 1;
  697. if (direntry->d_inode) {
  698. if (cifs_revalidate_dentry(direntry))
  699. return 0;
  700. } else {
  701. cFYI(1, "neg dentry 0x%p name = %s",
  702. direntry, direntry->d_name.name);
  703. if (time_after(jiffies, direntry->d_time + HZ) ||
  704. !lookupCacheEnabled) {
  705. d_drop(direntry);
  706. isValid = 0;
  707. }
  708. }
  709. return isValid;
  710. }
  711. /* static int cifs_d_delete(struct dentry *direntry)
  712. {
  713. int rc = 0;
  714. cFYI(1, "In cifs d_delete, name = %s", direntry->d_name.name);
  715. return rc;
  716. } */
  717. const struct dentry_operations cifs_dentry_ops = {
  718. .d_revalidate = cifs_d_revalidate,
  719. /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
  720. };
  721. static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
  722. {
  723. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  724. unsigned long hash;
  725. int i;
  726. hash = init_name_hash();
  727. for (i = 0; i < q->len; i++)
  728. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  729. hash);
  730. q->hash = end_name_hash(hash);
  731. return 0;
  732. }
  733. static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
  734. struct qstr *b)
  735. {
  736. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  737. if ((a->len == b->len) &&
  738. (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
  739. /*
  740. * To preserve case, don't let an existing negative dentry's
  741. * case take precedence. If a is not a negative dentry, this
  742. * should have no side effects
  743. */
  744. memcpy((void *)a->name, b->name, a->len);
  745. return 0;
  746. }
  747. return 1;
  748. }
  749. const struct dentry_operations cifs_ci_dentry_ops = {
  750. .d_revalidate = cifs_d_revalidate,
  751. .d_hash = cifs_ci_hash,
  752. .d_compare = cifs_ci_compare,
  753. };