dir.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * fs/cifs/dir.c
  3. *
  4. * vfs operations that deal with dentries
  5. *
  6. * Copyright (C) International Business Machines Corp., 2002,2008
  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. /* Inode operations in similar order to how they appear in Linux file fs.h */
  126. int
  127. cifs_create(struct inode *inode, struct dentry *direntry, int mode,
  128. struct nameidata *nd)
  129. {
  130. int rc = -ENOENT;
  131. int xid;
  132. int create_options = CREATE_NOT_DIR;
  133. int oplock = 0;
  134. int desiredAccess = GENERIC_READ | GENERIC_WRITE;
  135. __u16 fileHandle;
  136. struct cifs_sb_info *cifs_sb;
  137. struct cifsTconInfo *pTcon;
  138. char *full_path = NULL;
  139. FILE_ALL_INFO *buf = NULL;
  140. struct inode *newinode = NULL;
  141. struct cifsFileInfo *pCifsFile = NULL;
  142. struct cifsInodeInfo *pCifsInode;
  143. int disposition = FILE_OVERWRITE_IF;
  144. bool write_only = false;
  145. xid = GetXid();
  146. cifs_sb = CIFS_SB(inode->i_sb);
  147. pTcon = cifs_sb->tcon;
  148. full_path = build_path_from_dentry(direntry);
  149. if (full_path == NULL) {
  150. FreeXid(xid);
  151. return -ENOMEM;
  152. }
  153. if (nd && (nd->flags & LOOKUP_OPEN)) {
  154. int oflags = nd->intent.open.flags;
  155. desiredAccess = 0;
  156. if (oflags & FMODE_READ)
  157. desiredAccess |= GENERIC_READ;
  158. if (oflags & FMODE_WRITE) {
  159. desiredAccess |= GENERIC_WRITE;
  160. if (!(oflags & FMODE_READ))
  161. write_only = true;
  162. }
  163. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  164. disposition = FILE_CREATE;
  165. else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  166. disposition = FILE_OVERWRITE_IF;
  167. else if ((oflags & O_CREAT) == O_CREAT)
  168. disposition = FILE_OPEN_IF;
  169. else
  170. cFYI(1, ("Create flag not set in create function"));
  171. }
  172. /* BB add processing to set equivalent of mode - e.g. via CreateX with
  173. ACLs */
  174. if (oplockEnabled)
  175. oplock = REQ_OPLOCK;
  176. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  177. if (buf == NULL) {
  178. kfree(full_path);
  179. FreeXid(xid);
  180. return -ENOMEM;
  181. }
  182. mode &= ~current->fs->umask;
  183. /*
  184. * if we're not using unix extensions, see if we need to set
  185. * ATTR_READONLY on the create call
  186. */
  187. if (!pTcon->unix_ext && (mode & S_IWUGO) == 0)
  188. create_options |= CREATE_OPTION_READONLY;
  189. if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS)
  190. rc = CIFSSMBOpen(xid, pTcon, full_path, disposition,
  191. desiredAccess, create_options,
  192. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  193. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  194. else
  195. rc = -EIO; /* no NT SMB support fall into legacy open below */
  196. if (rc == -EIO) {
  197. /* old server, retry the open legacy style */
  198. rc = SMBLegacyOpen(xid, pTcon, full_path, disposition,
  199. desiredAccess, create_options,
  200. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  201. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  202. }
  203. if (rc) {
  204. cFYI(1, ("cifs_create returned 0x%x", rc));
  205. } else {
  206. /* If Open reported that we actually created a file
  207. then we now have to set the mode if possible */
  208. if ((pTcon->unix_ext) && (oplock & CIFS_CREATE_ACTION)) {
  209. struct cifs_unix_set_info_args args = {
  210. .mode = mode,
  211. .ctime = NO_CHANGE_64,
  212. .atime = NO_CHANGE_64,
  213. .mtime = NO_CHANGE_64,
  214. .device = 0,
  215. };
  216. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  217. args.uid = (__u64) current_fsuid();
  218. if (inode->i_mode & S_ISGID)
  219. args.gid = (__u64) inode->i_gid;
  220. else
  221. args.gid = (__u64) current_fsgid();
  222. } else {
  223. args.uid = NO_CHANGE_64;
  224. args.gid = NO_CHANGE_64;
  225. }
  226. CIFSSMBUnixSetInfo(xid, pTcon, full_path, &args,
  227. cifs_sb->local_nls,
  228. cifs_sb->mnt_cifs_flags &
  229. CIFS_MOUNT_MAP_SPECIAL_CHR);
  230. } else {
  231. /* BB implement mode setting via Windows security
  232. descriptors e.g. */
  233. /* CIFSSMBWinSetPerms(xid,pTcon,path,mode,-1,-1,nls);*/
  234. /* Could set r/o dos attribute if mode & 0222 == 0 */
  235. }
  236. /* server might mask mode so we have to query for it */
  237. if (pTcon->unix_ext)
  238. rc = cifs_get_inode_info_unix(&newinode, full_path,
  239. inode->i_sb, xid);
  240. else {
  241. rc = cifs_get_inode_info(&newinode, full_path,
  242. buf, inode->i_sb, xid,
  243. &fileHandle);
  244. if (newinode) {
  245. if (cifs_sb->mnt_cifs_flags &
  246. CIFS_MOUNT_DYNPERM)
  247. newinode->i_mode = mode;
  248. if ((oplock & CIFS_CREATE_ACTION) &&
  249. (cifs_sb->mnt_cifs_flags &
  250. CIFS_MOUNT_SET_UID)) {
  251. newinode->i_uid = current_fsuid();
  252. if (inode->i_mode & S_ISGID)
  253. newinode->i_gid =
  254. inode->i_gid;
  255. else
  256. newinode->i_gid =
  257. current_fsgid();
  258. }
  259. }
  260. }
  261. if (rc != 0) {
  262. cFYI(1,
  263. ("Create worked but get_inode_info failed rc = %d",
  264. rc));
  265. } else {
  266. if (pTcon->nocase)
  267. direntry->d_op = &cifs_ci_dentry_ops;
  268. else
  269. direntry->d_op = &cifs_dentry_ops;
  270. d_instantiate(direntry, newinode);
  271. }
  272. if ((nd == NULL /* nfsd case - nfs srv does not set nd */) ||
  273. (!(nd->flags & LOOKUP_OPEN))) {
  274. /* mknod case - do not leave file open */
  275. CIFSSMBClose(xid, pTcon, fileHandle);
  276. } else if (newinode) {
  277. pCifsFile =
  278. kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  279. if (pCifsFile == NULL)
  280. goto cifs_create_out;
  281. pCifsFile->netfid = fileHandle;
  282. pCifsFile->pid = current->tgid;
  283. pCifsFile->pInode = newinode;
  284. pCifsFile->invalidHandle = false;
  285. pCifsFile->closePend = false;
  286. init_MUTEX(&pCifsFile->fh_sem);
  287. mutex_init(&pCifsFile->lock_mutex);
  288. INIT_LIST_HEAD(&pCifsFile->llist);
  289. atomic_set(&pCifsFile->wrtPending, 0);
  290. /* set the following in open now
  291. pCifsFile->pfile = file; */
  292. write_lock(&GlobalSMBSeslock);
  293. list_add(&pCifsFile->tlist, &pTcon->openFileList);
  294. pCifsInode = CIFS_I(newinode);
  295. if (pCifsInode) {
  296. /* if readable file instance put first in list*/
  297. if (write_only) {
  298. list_add_tail(&pCifsFile->flist,
  299. &pCifsInode->openFileList);
  300. } else {
  301. list_add(&pCifsFile->flist,
  302. &pCifsInode->openFileList);
  303. }
  304. if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  305. pCifsInode->clientCanCacheAll = true;
  306. pCifsInode->clientCanCacheRead = true;
  307. cFYI(1, ("Exclusive Oplock inode %p",
  308. newinode));
  309. } else if ((oplock & 0xF) == OPLOCK_READ)
  310. pCifsInode->clientCanCacheRead = true;
  311. }
  312. write_unlock(&GlobalSMBSeslock);
  313. }
  314. }
  315. cifs_create_out:
  316. kfree(buf);
  317. kfree(full_path);
  318. FreeXid(xid);
  319. return rc;
  320. }
  321. int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode,
  322. dev_t device_number)
  323. {
  324. int rc = -EPERM;
  325. int xid;
  326. struct cifs_sb_info *cifs_sb;
  327. struct cifsTconInfo *pTcon;
  328. char *full_path = NULL;
  329. struct inode *newinode = NULL;
  330. if (!old_valid_dev(device_number))
  331. return -EINVAL;
  332. xid = GetXid();
  333. cifs_sb = CIFS_SB(inode->i_sb);
  334. pTcon = cifs_sb->tcon;
  335. full_path = build_path_from_dentry(direntry);
  336. if (full_path == NULL)
  337. rc = -ENOMEM;
  338. else if (pTcon->unix_ext) {
  339. struct cifs_unix_set_info_args args = {
  340. .mode = mode & ~current->fs->umask,
  341. .ctime = NO_CHANGE_64,
  342. .atime = NO_CHANGE_64,
  343. .mtime = NO_CHANGE_64,
  344. .device = device_number,
  345. };
  346. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  347. args.uid = (__u64) current_fsuid();
  348. args.gid = (__u64) current_fsgid();
  349. } else {
  350. args.uid = NO_CHANGE_64;
  351. args.gid = NO_CHANGE_64;
  352. }
  353. rc = CIFSSMBUnixSetInfo(xid, pTcon, full_path,
  354. &args, cifs_sb->local_nls,
  355. cifs_sb->mnt_cifs_flags &
  356. CIFS_MOUNT_MAP_SPECIAL_CHR);
  357. if (!rc) {
  358. rc = cifs_get_inode_info_unix(&newinode, full_path,
  359. inode->i_sb, xid);
  360. if (pTcon->nocase)
  361. direntry->d_op = &cifs_ci_dentry_ops;
  362. else
  363. direntry->d_op = &cifs_dentry_ops;
  364. if (rc == 0)
  365. d_instantiate(direntry, newinode);
  366. }
  367. } else {
  368. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  369. int oplock = 0;
  370. u16 fileHandle;
  371. FILE_ALL_INFO *buf;
  372. cFYI(1, ("sfu compat create special file"));
  373. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  374. if (buf == NULL) {
  375. kfree(full_path);
  376. FreeXid(xid);
  377. return -ENOMEM;
  378. }
  379. rc = CIFSSMBOpen(xid, pTcon, full_path,
  380. FILE_CREATE, /* fail if exists */
  381. GENERIC_WRITE /* BB would
  382. WRITE_OWNER | WRITE_DAC be better? */,
  383. /* Create a file and set the
  384. file attribute to SYSTEM */
  385. CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
  386. &fileHandle, &oplock, buf,
  387. cifs_sb->local_nls,
  388. cifs_sb->mnt_cifs_flags &
  389. CIFS_MOUNT_MAP_SPECIAL_CHR);
  390. /* BB FIXME - add handling for backlevel servers
  391. which need legacy open and check for all
  392. calls to SMBOpen for fallback to SMBLeagcyOpen */
  393. if (!rc) {
  394. /* BB Do not bother to decode buf since no
  395. local inode yet to put timestamps in,
  396. but we can reuse it safely */
  397. unsigned int bytes_written;
  398. struct win_dev *pdev;
  399. pdev = (struct win_dev *)buf;
  400. if (S_ISCHR(mode)) {
  401. memcpy(pdev->type, "IntxCHR", 8);
  402. pdev->major =
  403. cpu_to_le64(MAJOR(device_number));
  404. pdev->minor =
  405. cpu_to_le64(MINOR(device_number));
  406. rc = CIFSSMBWrite(xid, pTcon,
  407. fileHandle,
  408. sizeof(struct win_dev),
  409. 0, &bytes_written, (char *)pdev,
  410. NULL, 0);
  411. } else if (S_ISBLK(mode)) {
  412. memcpy(pdev->type, "IntxBLK", 8);
  413. pdev->major =
  414. cpu_to_le64(MAJOR(device_number));
  415. pdev->minor =
  416. cpu_to_le64(MINOR(device_number));
  417. rc = CIFSSMBWrite(xid, pTcon,
  418. fileHandle,
  419. sizeof(struct win_dev),
  420. 0, &bytes_written, (char *)pdev,
  421. NULL, 0);
  422. } /* else if(S_ISFIFO */
  423. CIFSSMBClose(xid, pTcon, fileHandle);
  424. d_drop(direntry);
  425. }
  426. kfree(buf);
  427. /* add code here to set EAs */
  428. }
  429. }
  430. kfree(full_path);
  431. FreeXid(xid);
  432. return rc;
  433. }
  434. struct dentry *
  435. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
  436. struct nameidata *nd)
  437. {
  438. int xid;
  439. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  440. struct cifs_sb_info *cifs_sb;
  441. struct cifsTconInfo *pTcon;
  442. struct inode *newInode = NULL;
  443. char *full_path = NULL;
  444. xid = GetXid();
  445. cFYI(1, ("parent inode = 0x%p name is: %s and dentry = 0x%p",
  446. parent_dir_inode, direntry->d_name.name, direntry));
  447. /* check whether path exists */
  448. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  449. pTcon = cifs_sb->tcon;
  450. /*
  451. * Don't allow the separator character in a path component.
  452. * The VFS will not allow "/", but "\" is allowed by posix.
  453. */
  454. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
  455. int i;
  456. for (i = 0; i < direntry->d_name.len; i++)
  457. if (direntry->d_name.name[i] == '\\') {
  458. cFYI(1, ("Invalid file name"));
  459. FreeXid(xid);
  460. return ERR_PTR(-EINVAL);
  461. }
  462. }
  463. /* can not grab the rename sem here since it would
  464. deadlock in the cases (beginning of sys_rename itself)
  465. in which we already have the sb rename sem */
  466. full_path = build_path_from_dentry(direntry);
  467. if (full_path == NULL) {
  468. FreeXid(xid);
  469. return ERR_PTR(-ENOMEM);
  470. }
  471. if (direntry->d_inode != NULL) {
  472. cFYI(1, ("non-NULL inode in lookup"));
  473. } else {
  474. cFYI(1, ("NULL inode in lookup"));
  475. }
  476. cFYI(1, ("Full path: %s inode = 0x%p", full_path, direntry->d_inode));
  477. if (pTcon->unix_ext)
  478. rc = cifs_get_inode_info_unix(&newInode, full_path,
  479. parent_dir_inode->i_sb, xid);
  480. else
  481. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  482. parent_dir_inode->i_sb, xid, NULL);
  483. if ((rc == 0) && (newInode != NULL)) {
  484. if (pTcon->nocase)
  485. direntry->d_op = &cifs_ci_dentry_ops;
  486. else
  487. direntry->d_op = &cifs_dentry_ops;
  488. d_add(direntry, newInode);
  489. /* since paths are not looked up by component - the parent
  490. directories are presumed to be good here */
  491. renew_parental_timestamps(direntry);
  492. } else if (rc == -ENOENT) {
  493. rc = 0;
  494. direntry->d_time = jiffies;
  495. if (pTcon->nocase)
  496. direntry->d_op = &cifs_ci_dentry_ops;
  497. else
  498. direntry->d_op = &cifs_dentry_ops;
  499. d_add(direntry, NULL);
  500. /* if it was once a directory (but how can we tell?) we could do
  501. shrink_dcache_parent(direntry); */
  502. } else if (rc != -EACCES) {
  503. cERROR(1, ("Unexpected lookup error %d", rc));
  504. /* We special case check for Access Denied - since that
  505. is a common return code */
  506. }
  507. kfree(full_path);
  508. FreeXid(xid);
  509. return ERR_PTR(rc);
  510. }
  511. static int
  512. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  513. {
  514. int isValid = 1;
  515. if (direntry->d_inode) {
  516. if (cifs_revalidate(direntry))
  517. return 0;
  518. } else {
  519. cFYI(1, ("neg dentry 0x%p name = %s",
  520. direntry, direntry->d_name.name));
  521. if (time_after(jiffies, direntry->d_time + HZ) ||
  522. !lookupCacheEnabled) {
  523. d_drop(direntry);
  524. isValid = 0;
  525. }
  526. }
  527. return isValid;
  528. }
  529. /* static int cifs_d_delete(struct dentry *direntry)
  530. {
  531. int rc = 0;
  532. cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
  533. return rc;
  534. } */
  535. struct dentry_operations cifs_dentry_ops = {
  536. .d_revalidate = cifs_d_revalidate,
  537. /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
  538. };
  539. static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
  540. {
  541. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  542. unsigned long hash;
  543. int i;
  544. hash = init_name_hash();
  545. for (i = 0; i < q->len; i++)
  546. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  547. hash);
  548. q->hash = end_name_hash(hash);
  549. return 0;
  550. }
  551. static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
  552. struct qstr *b)
  553. {
  554. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  555. if ((a->len == b->len) &&
  556. (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
  557. /*
  558. * To preserve case, don't let an existing negative dentry's
  559. * case take precedence. If a is not a negative dentry, this
  560. * should have no side effects
  561. */
  562. memcpy((void *)a->name, b->name, a->len);
  563. return 0;
  564. }
  565. return 1;
  566. }
  567. struct dentry_operations cifs_ci_dentry_ops = {
  568. .d_revalidate = cifs_d_revalidate,
  569. .d_hash = cifs_ci_hash,
  570. .d_compare = cifs_ci_compare,
  571. };