dir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 "cifsfs.h"
  29. #include "cifspdu.h"
  30. #include "cifsglob.h"
  31. #include "cifsproto.h"
  32. #include "cifs_debug.h"
  33. #include "cifs_fs_sb.h"
  34. static void
  35. renew_parental_timestamps(struct dentry *direntry)
  36. {
  37. /* BB check if there is a way to get the kernel to do this or if we
  38. really need this */
  39. do {
  40. direntry->d_time = jiffies;
  41. direntry = direntry->d_parent;
  42. } while (!IS_ROOT(direntry));
  43. }
  44. /* Note: caller must free return buffer */
  45. char *
  46. build_path_from_dentry(struct dentry *direntry)
  47. {
  48. struct dentry *temp;
  49. int namelen;
  50. int pplen;
  51. int dfsplen;
  52. char *full_path;
  53. char dirsep;
  54. struct cifs_sb_info *cifs_sb;
  55. if (direntry == NULL)
  56. return NULL; /* not much we can do if dentry is freed and
  57. we need to reopen the file after it was closed implicitly
  58. when the server crashed */
  59. cifs_sb = CIFS_SB(direntry->d_sb);
  60. dirsep = CIFS_DIR_SEP(cifs_sb);
  61. pplen = cifs_sb->prepathlen;
  62. if (cifs_sb->tcon && (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS))
  63. dfsplen = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE + 1);
  64. else
  65. dfsplen = 0;
  66. cifs_bp_rename_retry:
  67. namelen = pplen + dfsplen;
  68. for (temp = direntry; !IS_ROOT(temp);) {
  69. namelen += (1 + temp->d_name.len);
  70. temp = temp->d_parent;
  71. if (temp == NULL) {
  72. cERROR(1, ("corrupt dentry"));
  73. return NULL;
  74. }
  75. }
  76. full_path = kmalloc(namelen+1, GFP_KERNEL);
  77. if (full_path == NULL)
  78. return full_path;
  79. full_path[namelen] = 0; /* trailing null */
  80. for (temp = direntry; !IS_ROOT(temp);) {
  81. namelen -= 1 + temp->d_name.len;
  82. if (namelen < 0) {
  83. break;
  84. } else {
  85. full_path[namelen] = dirsep;
  86. strncpy(full_path + namelen + 1, temp->d_name.name,
  87. temp->d_name.len);
  88. cFYI(0, ("name: %s", full_path + namelen));
  89. }
  90. temp = temp->d_parent;
  91. if (temp == NULL) {
  92. cERROR(1, ("corrupt dentry"));
  93. kfree(full_path);
  94. return NULL;
  95. }
  96. }
  97. if (namelen != pplen + dfsplen) {
  98. cERROR(1,
  99. ("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. return pCifsFile;
  170. }
  171. int cifs_posix_open(char *full_path, struct inode **pinode,
  172. struct vfsmount *mnt, 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(mnt->mnt_sb);
  179. struct cifs_fattr fattr;
  180. cFYI(1, ("posix open %s", full_path));
  181. presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  182. if (presp_data == NULL)
  183. return -ENOMEM;
  184. /* So far cifs posix extensions can only map the following flags.
  185. There are other valid fmode oflags such as FMODE_LSEEK, FMODE_PREAD, but
  186. so far we do not seem to need them, and we can treat them as local only */
  187. if ((oflags & (FMODE_READ | FMODE_WRITE)) ==
  188. (FMODE_READ | FMODE_WRITE))
  189. posix_flags = SMB_O_RDWR;
  190. else if (oflags & FMODE_READ)
  191. posix_flags = SMB_O_RDONLY;
  192. else if (oflags & FMODE_WRITE)
  193. posix_flags = SMB_O_WRONLY;
  194. if (oflags & O_CREAT)
  195. posix_flags |= SMB_O_CREAT;
  196. if (oflags & O_EXCL)
  197. posix_flags |= SMB_O_EXCL;
  198. if (oflags & O_TRUNC)
  199. posix_flags |= SMB_O_TRUNC;
  200. if (oflags & O_APPEND)
  201. posix_flags |= SMB_O_APPEND;
  202. if (oflags & O_SYNC)
  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. *pinode = cifs_iget(mnt->mnt_sb, &fattr);
  225. if (!*pinode) {
  226. rc = -ENOMEM;
  227. goto posix_open_ret;
  228. }
  229. } else {
  230. cifs_fattr_to_inode(*pinode, &fattr);
  231. }
  232. cifs_new_fileinfo(*pinode, *pnetfid, NULL, mnt, oflags);
  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. bool posix_create = false;
  258. /*
  259. * BB below access is probably too much for mknod to request
  260. * but we have to do query and setpathinfo so requesting
  261. * less could fail (unless we want to request getatr and setatr
  262. * permissions (only). At least for POSIX we do not have to
  263. * request so much.
  264. */
  265. int desiredAccess = GENERIC_READ | GENERIC_WRITE;
  266. __u16 fileHandle;
  267. struct cifs_sb_info *cifs_sb;
  268. struct cifsTconInfo *tcon;
  269. char *full_path = NULL;
  270. FILE_ALL_INFO *buf = NULL;
  271. struct inode *newinode = NULL;
  272. int disposition = FILE_OVERWRITE_IF;
  273. xid = GetXid();
  274. cifs_sb = CIFS_SB(inode->i_sb);
  275. tcon = cifs_sb->tcon;
  276. full_path = build_path_from_dentry(direntry);
  277. if (full_path == NULL) {
  278. rc = -ENOMEM;
  279. FreeXid(xid);
  280. return rc;
  281. }
  282. if (oplockEnabled)
  283. oplock = REQ_OPLOCK;
  284. if (nd && (nd->flags & LOOKUP_OPEN))
  285. oflags = nd->intent.open.flags;
  286. else
  287. oflags = FMODE_READ;
  288. if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) &&
  289. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  290. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  291. rc = cifs_posix_open(full_path, &newinode, nd->path.mnt,
  292. mode, oflags, &oplock, &fileHandle, xid);
  293. /* EIO could indicate that (posix open) operation is not
  294. supported, despite what server claimed in capability
  295. negotation. EREMOTE indicates DFS junction, which is not
  296. handled in posix open */
  297. if (rc == 0) {
  298. posix_create = true;
  299. if (newinode == NULL) /* query inode info */
  300. goto cifs_create_get_file_info;
  301. else /* success, no need to query */
  302. goto cifs_create_set_dentry;
  303. } else if ((rc != -EIO) && (rc != -EREMOTE) &&
  304. (rc != -EOPNOTSUPP) && (rc != -EINVAL))
  305. goto cifs_create_out;
  306. /* else fallthrough to retry, using older open call, this is
  307. case where server does not support this SMB level, and
  308. falsely claims capability (also get here for DFS case
  309. which should be rare for path not covered on files) */
  310. }
  311. if (nd && (nd->flags & LOOKUP_OPEN)) {
  312. /* if the file is going to stay open, then we
  313. need to set the desired access properly */
  314. desiredAccess = 0;
  315. if (oflags & FMODE_READ)
  316. desiredAccess |= GENERIC_READ; /* is this too little? */
  317. if (oflags & FMODE_WRITE)
  318. desiredAccess |= GENERIC_WRITE;
  319. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  320. disposition = FILE_CREATE;
  321. else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  322. disposition = FILE_OVERWRITE_IF;
  323. else if ((oflags & O_CREAT) == O_CREAT)
  324. disposition = FILE_OPEN_IF;
  325. else
  326. cFYI(1, ("Create flag not set in create function"));
  327. }
  328. /* BB add processing to set equivalent of mode - e.g. via CreateX with
  329. ACLs */
  330. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  331. if (buf == NULL) {
  332. kfree(full_path);
  333. FreeXid(xid);
  334. return -ENOMEM;
  335. }
  336. /*
  337. * if we're not using unix extensions, see if we need to set
  338. * ATTR_READONLY on the create call
  339. */
  340. if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
  341. create_options |= CREATE_OPTION_READONLY;
  342. if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS)
  343. rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
  344. desiredAccess, create_options,
  345. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  346. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  347. else
  348. rc = -EIO; /* no NT SMB support fall into legacy open below */
  349. if (rc == -EIO) {
  350. /* old server, retry the open legacy style */
  351. rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
  352. desiredAccess, create_options,
  353. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  354. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  355. }
  356. if (rc) {
  357. cFYI(1, ("cifs_create returned 0x%x", rc));
  358. goto cifs_create_out;
  359. }
  360. /* If Open reported that we actually created a file
  361. then we now have to set the mode if possible */
  362. if ((tcon->unix_ext) && (oplock & CIFS_CREATE_ACTION)) {
  363. struct cifs_unix_set_info_args args = {
  364. .mode = mode,
  365. .ctime = NO_CHANGE_64,
  366. .atime = NO_CHANGE_64,
  367. .mtime = NO_CHANGE_64,
  368. .device = 0,
  369. };
  370. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  371. args.uid = (__u64) current_fsuid();
  372. if (inode->i_mode & S_ISGID)
  373. args.gid = (__u64) inode->i_gid;
  374. else
  375. args.gid = (__u64) current_fsgid();
  376. } else {
  377. args.uid = NO_CHANGE_64;
  378. args.gid = NO_CHANGE_64;
  379. }
  380. CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
  381. cifs_sb->local_nls,
  382. cifs_sb->mnt_cifs_flags &
  383. CIFS_MOUNT_MAP_SPECIAL_CHR);
  384. } else {
  385. /* BB implement mode setting via Windows security
  386. descriptors e.g. */
  387. /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
  388. /* Could set r/o dos attribute if mode & 0222 == 0 */
  389. }
  390. cifs_create_get_file_info:
  391. /* server might mask mode so we have to query for it */
  392. if (tcon->unix_ext)
  393. rc = cifs_get_inode_info_unix(&newinode, full_path,
  394. inode->i_sb, xid);
  395. else {
  396. rc = cifs_get_inode_info(&newinode, full_path, buf,
  397. inode->i_sb, xid, &fileHandle);
  398. if (newinode) {
  399. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  400. newinode->i_mode = mode;
  401. if ((oplock & CIFS_CREATE_ACTION) &&
  402. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) {
  403. newinode->i_uid = current_fsuid();
  404. if (inode->i_mode & S_ISGID)
  405. newinode->i_gid = inode->i_gid;
  406. else
  407. newinode->i_gid = current_fsgid();
  408. }
  409. }
  410. }
  411. cifs_create_set_dentry:
  412. if (rc == 0)
  413. setup_cifs_dentry(tcon, direntry, newinode);
  414. else
  415. cFYI(1, ("Create worked, get_inode_info failed rc = %d", rc));
  416. /* nfsd case - nfs srv does not set nd */
  417. if ((nd == NULL) || (!(nd->flags & LOOKUP_OPEN))) {
  418. /* mknod case - do not leave file open */
  419. CIFSSMBClose(xid, tcon, fileHandle);
  420. } else if (!(posix_create) && (newinode)) {
  421. cifs_new_fileinfo(newinode, fileHandle, NULL,
  422. nd->path.mnt, oflags);
  423. }
  424. cifs_create_out:
  425. kfree(buf);
  426. kfree(full_path);
  427. FreeXid(xid);
  428. return rc;
  429. }
  430. int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode,
  431. dev_t device_number)
  432. {
  433. int rc = -EPERM;
  434. int xid;
  435. struct cifs_sb_info *cifs_sb;
  436. struct cifsTconInfo *pTcon;
  437. char *full_path = NULL;
  438. struct inode *newinode = NULL;
  439. if (!old_valid_dev(device_number))
  440. return -EINVAL;
  441. xid = GetXid();
  442. cifs_sb = CIFS_SB(inode->i_sb);
  443. pTcon = cifs_sb->tcon;
  444. full_path = build_path_from_dentry(direntry);
  445. if (full_path == NULL)
  446. rc = -ENOMEM;
  447. else if (pTcon->unix_ext) {
  448. struct cifs_unix_set_info_args args = {
  449. .mode = mode & ~current_umask(),
  450. .ctime = NO_CHANGE_64,
  451. .atime = NO_CHANGE_64,
  452. .mtime = NO_CHANGE_64,
  453. .device = device_number,
  454. };
  455. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  456. args.uid = (__u64) current_fsuid();
  457. args.gid = (__u64) current_fsgid();
  458. } else {
  459. args.uid = NO_CHANGE_64;
  460. args.gid = NO_CHANGE_64;
  461. }
  462. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args,
  463. cifs_sb->local_nls,
  464. cifs_sb->mnt_cifs_flags &
  465. CIFS_MOUNT_MAP_SPECIAL_CHR);
  466. if (!rc) {
  467. rc = cifs_get_inode_info_unix(&newinode, full_path,
  468. inode->i_sb, xid);
  469. if (pTcon->nocase)
  470. direntry->d_op = &cifs_ci_dentry_ops;
  471. else
  472. direntry->d_op = &cifs_dentry_ops;
  473. if (rc == 0)
  474. d_instantiate(direntry, newinode);
  475. }
  476. } else {
  477. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  478. int oplock = 0;
  479. u16 fileHandle;
  480. FILE_ALL_INFO *buf;
  481. cFYI(1, ("sfu compat create special file"));
  482. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  483. if (buf == NULL) {
  484. kfree(full_path);
  485. rc = -ENOMEM;
  486. FreeXid(xid);
  487. return rc;
  488. }
  489. rc = CIFSSMBOpen(xid, pTcon, full_path,
  490. FILE_CREATE, /* fail if exists */
  491. GENERIC_WRITE /* BB would
  492. WRITE_OWNER | WRITE_DAC be better? */,
  493. /* Create a file and set the
  494. file attribute to SYSTEM */
  495. CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
  496. &fileHandle, &oplock, buf,
  497. cifs_sb->local_nls,
  498. cifs_sb->mnt_cifs_flags &
  499. CIFS_MOUNT_MAP_SPECIAL_CHR);
  500. /* BB FIXME - add handling for backlevel servers
  501. which need legacy open and check for all
  502. calls to SMBOpen for fallback to SMBLeagcyOpen */
  503. if (!rc) {
  504. /* BB Do not bother to decode buf since no
  505. local inode yet to put timestamps in,
  506. but we can reuse it safely */
  507. unsigned int bytes_written;
  508. struct win_dev *pdev;
  509. pdev = (struct win_dev *)buf;
  510. if (S_ISCHR(mode)) {
  511. memcpy(pdev->type, "IntxCHR", 8);
  512. pdev->major =
  513. cpu_to_le64(MAJOR(device_number));
  514. pdev->minor =
  515. cpu_to_le64(MINOR(device_number));
  516. rc = CIFSSMBWrite(xid, pTcon,
  517. fileHandle,
  518. sizeof(struct win_dev),
  519. 0, &bytes_written, (char *)pdev,
  520. NULL, 0);
  521. } else if (S_ISBLK(mode)) {
  522. memcpy(pdev->type, "IntxBLK", 8);
  523. pdev->major =
  524. cpu_to_le64(MAJOR(device_number));
  525. pdev->minor =
  526. cpu_to_le64(MINOR(device_number));
  527. rc = CIFSSMBWrite(xid, pTcon,
  528. fileHandle,
  529. sizeof(struct win_dev),
  530. 0, &bytes_written, (char *)pdev,
  531. NULL, 0);
  532. } /* else if(S_ISFIFO */
  533. CIFSSMBClose(xid, pTcon, fileHandle);
  534. d_drop(direntry);
  535. }
  536. kfree(buf);
  537. /* add code here to set EAs */
  538. }
  539. }
  540. kfree(full_path);
  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 inode *newInode = NULL;
  556. char *full_path = NULL;
  557. struct file *filp;
  558. xid = GetXid();
  559. cFYI(1, ("parent inode = 0x%p name is: %s and dentry = 0x%p",
  560. parent_dir_inode, direntry->d_name.name, direntry));
  561. /* check whether path exists */
  562. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  563. pTcon = cifs_sb->tcon;
  564. /*
  565. * Don't allow the separator character in a path component.
  566. * The VFS will not allow "/", but "\" is allowed by posix.
  567. */
  568. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
  569. int i;
  570. for (i = 0; i < direntry->d_name.len; i++)
  571. if (direntry->d_name.name[i] == '\\') {
  572. cFYI(1, ("Invalid file name"));
  573. FreeXid(xid);
  574. return ERR_PTR(-EINVAL);
  575. }
  576. }
  577. /*
  578. * O_EXCL: optimize away the lookup, but don't hash the dentry. Let
  579. * the VFS handle the create.
  580. */
  581. if (nd->flags & LOOKUP_EXCL) {
  582. d_instantiate(direntry, NULL);
  583. return 0;
  584. }
  585. /* can not grab the rename sem here since it would
  586. deadlock in the cases (beginning of sys_rename itself)
  587. in which we already have the sb rename sem */
  588. full_path = build_path_from_dentry(direntry);
  589. if (full_path == NULL) {
  590. FreeXid(xid);
  591. return ERR_PTR(-ENOMEM);
  592. }
  593. if (direntry->d_inode != NULL) {
  594. cFYI(1, ("non-NULL inode in lookup"));
  595. } else {
  596. cFYI(1, ("NULL inode in lookup"));
  597. }
  598. cFYI(1, ("Full path: %s inode = 0x%p", full_path, direntry->d_inode));
  599. /* Posix open is only called (at lookup time) for file create now.
  600. * For opens (rather than creates), because we do not know if it
  601. * is a file or directory yet, and current Samba no longer allows
  602. * us to do posix open on dirs, we could end up wasting an open call
  603. * on what turns out to be a dir. For file opens, we wait to call posix
  604. * open till cifs_open. It could be added here (lookup) in the future
  605. * but the performance tradeoff of the extra network request when EISDIR
  606. * or EACCES is returned would have to be weighed against the 50%
  607. * reduction in network traffic in the other paths.
  608. */
  609. if (pTcon->unix_ext) {
  610. if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY)) &&
  611. (nd->flags & LOOKUP_OPEN) && !pTcon->broken_posix_open &&
  612. (nd->intent.open.flags & O_CREAT)) {
  613. rc = cifs_posix_open(full_path, &newInode, nd->path.mnt,
  614. nd->intent.open.create_mode,
  615. nd->intent.open.flags, &oplock,
  616. &fileHandle, xid);
  617. /*
  618. * The check below works around a bug in POSIX
  619. * open in samba versions 3.3.1 and earlier where
  620. * open could incorrectly fail with invalid parameter.
  621. * If either that or op not supported returned, follow
  622. * the normal lookup.
  623. */
  624. if ((rc == 0) || (rc == -ENOENT))
  625. posix_open = true;
  626. else if ((rc == -EINVAL) || (rc != -EOPNOTSUPP))
  627. pTcon->broken_posix_open = true;
  628. }
  629. if (!posix_open)
  630. rc = cifs_get_inode_info_unix(&newInode, full_path,
  631. parent_dir_inode->i_sb, xid);
  632. } else
  633. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  634. parent_dir_inode->i_sb, xid, NULL);
  635. if ((rc == 0) && (newInode != NULL)) {
  636. if (pTcon->nocase)
  637. direntry->d_op = &cifs_ci_dentry_ops;
  638. else
  639. direntry->d_op = &cifs_dentry_ops;
  640. d_add(direntry, newInode);
  641. if (posix_open)
  642. filp = lookup_instantiate_filp(nd, direntry, NULL);
  643. /* since paths are not looked up by component - the parent
  644. directories are presumed to be good here */
  645. renew_parental_timestamps(direntry);
  646. } else if (rc == -ENOENT) {
  647. rc = 0;
  648. direntry->d_time = jiffies;
  649. if (pTcon->nocase)
  650. direntry->d_op = &cifs_ci_dentry_ops;
  651. else
  652. direntry->d_op = &cifs_dentry_ops;
  653. d_add(direntry, NULL);
  654. /* if it was once a directory (but how can we tell?) we could do
  655. shrink_dcache_parent(direntry); */
  656. } else if (rc != -EACCES) {
  657. cERROR(1, ("Unexpected lookup error %d", rc));
  658. /* We special case check for Access Denied - since that
  659. is a common return code */
  660. }
  661. kfree(full_path);
  662. FreeXid(xid);
  663. return ERR_PTR(rc);
  664. }
  665. static int
  666. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  667. {
  668. int isValid = 1;
  669. if (direntry->d_inode) {
  670. if (cifs_revalidate(direntry))
  671. return 0;
  672. } else {
  673. cFYI(1, ("neg dentry 0x%p name = %s",
  674. direntry, direntry->d_name.name));
  675. if (time_after(jiffies, direntry->d_time + HZ) ||
  676. !lookupCacheEnabled) {
  677. d_drop(direntry);
  678. isValid = 0;
  679. }
  680. }
  681. return isValid;
  682. }
  683. /* static int cifs_d_delete(struct dentry *direntry)
  684. {
  685. int rc = 0;
  686. cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
  687. return rc;
  688. } */
  689. const struct dentry_operations cifs_dentry_ops = {
  690. .d_revalidate = cifs_d_revalidate,
  691. /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
  692. };
  693. static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
  694. {
  695. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  696. unsigned long hash;
  697. int i;
  698. hash = init_name_hash();
  699. for (i = 0; i < q->len; i++)
  700. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  701. hash);
  702. q->hash = end_name_hash(hash);
  703. return 0;
  704. }
  705. static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
  706. struct qstr *b)
  707. {
  708. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  709. if ((a->len == b->len) &&
  710. (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
  711. /*
  712. * To preserve case, don't let an existing negative dentry's
  713. * case take precedence. If a is not a negative dentry, this
  714. * should have no side effects
  715. */
  716. memcpy((void *)a->name, b->name, a->len);
  717. return 0;
  718. }
  719. return 1;
  720. }
  721. const struct dentry_operations cifs_ci_dentry_ops = {
  722. .d_revalidate = cifs_d_revalidate,
  723. .d_hash = cifs_ci_hash,
  724. .d_compare = cifs_ci_compare,
  725. };