dir.c 21 KB

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