dir.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. char *full_path;
  51. char dirsep;
  52. if (direntry == NULL)
  53. return NULL; /* not much we can do if dentry is freed and
  54. we need to reopen the file after it was closed implicitly
  55. when the server crashed */
  56. dirsep = CIFS_DIR_SEP(CIFS_SB(direntry->d_sb));
  57. pplen = CIFS_SB(direntry->d_sb)->prepathlen;
  58. cifs_bp_rename_retry:
  59. namelen = pplen;
  60. for (temp = direntry; !IS_ROOT(temp);) {
  61. namelen += (1 + temp->d_name.len);
  62. temp = temp->d_parent;
  63. if (temp == NULL) {
  64. cERROR(1, ("corrupt dentry"));
  65. return NULL;
  66. }
  67. }
  68. full_path = kmalloc(namelen+1, GFP_KERNEL);
  69. if (full_path == NULL)
  70. return full_path;
  71. full_path[namelen] = 0; /* trailing null */
  72. for (temp = direntry; !IS_ROOT(temp);) {
  73. namelen -= 1 + temp->d_name.len;
  74. if (namelen < 0) {
  75. break;
  76. } else {
  77. full_path[namelen] = dirsep;
  78. strncpy(full_path + namelen + 1, temp->d_name.name,
  79. temp->d_name.len);
  80. cFYI(0, ("name: %s", full_path + namelen));
  81. }
  82. temp = temp->d_parent;
  83. if (temp == NULL) {
  84. cERROR(1, ("corrupt dentry"));
  85. kfree(full_path);
  86. return NULL;
  87. }
  88. }
  89. if (namelen != pplen) {
  90. cERROR(1,
  91. ("did not end path lookup where expected namelen is %d",
  92. namelen));
  93. /* presumably this is only possible if racing with a rename
  94. of one of the parent directories (we can not lock the dentries
  95. above us to prevent this, but retrying should be harmless) */
  96. kfree(full_path);
  97. goto cifs_bp_rename_retry;
  98. }
  99. /* DIR_SEP already set for byte 0 / vs \ but not for
  100. subsequent slashes in prepath which currently must
  101. be entered the right way - not sure if there is an alternative
  102. since the '\' is a valid posix character so we can not switch
  103. those safely to '/' if any are found in the middle of the prepath */
  104. /* BB test paths to Windows with '/' in the midst of prepath */
  105. strncpy(full_path, CIFS_SB(direntry->d_sb)->prepath, pplen);
  106. return full_path;
  107. }
  108. /* Inode operations in similar order to how they appear in Linux file fs.h */
  109. int
  110. cifs_create(struct inode *inode, struct dentry *direntry, int mode,
  111. struct nameidata *nd)
  112. {
  113. int rc = -ENOENT;
  114. int xid;
  115. int oplock = 0;
  116. int desiredAccess = GENERIC_READ | GENERIC_WRITE;
  117. __u16 fileHandle;
  118. struct cifs_sb_info *cifs_sb;
  119. struct cifsTconInfo *pTcon;
  120. char *full_path = NULL;
  121. FILE_ALL_INFO *buf = NULL;
  122. struct inode *newinode = NULL;
  123. struct cifsFileInfo *pCifsFile = NULL;
  124. struct cifsInodeInfo *pCifsInode;
  125. int disposition = FILE_OVERWRITE_IF;
  126. int write_only = FALSE;
  127. xid = GetXid();
  128. cifs_sb = CIFS_SB(inode->i_sb);
  129. pTcon = cifs_sb->tcon;
  130. full_path = build_path_from_dentry(direntry);
  131. if (full_path == NULL) {
  132. FreeXid(xid);
  133. return -ENOMEM;
  134. }
  135. if (nd && (nd->flags & LOOKUP_OPEN)) {
  136. int oflags = nd->intent.open.flags;
  137. desiredAccess = 0;
  138. if (oflags & FMODE_READ)
  139. desiredAccess |= GENERIC_READ;
  140. if (oflags & FMODE_WRITE) {
  141. desiredAccess |= GENERIC_WRITE;
  142. if (!(oflags & FMODE_READ))
  143. write_only = TRUE;
  144. }
  145. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  146. disposition = FILE_CREATE;
  147. else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  148. disposition = FILE_OVERWRITE_IF;
  149. else if ((oflags & O_CREAT) == O_CREAT)
  150. disposition = FILE_OPEN_IF;
  151. else
  152. cFYI(1, ("Create flag not set in create function"));
  153. }
  154. /* BB add processing to set equivalent of mode - e.g. via CreateX with
  155. ACLs */
  156. if (oplockEnabled)
  157. oplock = REQ_OPLOCK;
  158. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  159. if (buf == NULL) {
  160. kfree(full_path);
  161. FreeXid(xid);
  162. return -ENOMEM;
  163. }
  164. if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS)
  165. rc = CIFSSMBOpen(xid, pTcon, full_path, disposition,
  166. desiredAccess, CREATE_NOT_DIR,
  167. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  168. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  169. else
  170. rc = -EIO; /* no NT SMB support fall into legacy open below */
  171. if (rc == -EIO) {
  172. /* old server, retry the open legacy style */
  173. rc = SMBLegacyOpen(xid, pTcon, full_path, disposition,
  174. desiredAccess, CREATE_NOT_DIR,
  175. &fileHandle, &oplock, buf, cifs_sb->local_nls,
  176. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
  177. }
  178. if (rc) {
  179. cFYI(1, ("cifs_create returned 0x%x", rc));
  180. } else {
  181. /* If Open reported that we actually created a file
  182. then we now have to set the mode if possible */
  183. if ((pTcon->unix_ext) && (oplock & CIFS_CREATE_ACTION)) {
  184. mode &= ~current->fs->umask;
  185. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  186. CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode,
  187. (__u64)current->fsuid,
  188. (__u64)current->fsgid,
  189. 0 /* dev */,
  190. cifs_sb->local_nls,
  191. cifs_sb->mnt_cifs_flags &
  192. CIFS_MOUNT_MAP_SPECIAL_CHR);
  193. } else {
  194. CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode,
  195. (__u64)-1,
  196. (__u64)-1,
  197. 0 /* dev */,
  198. cifs_sb->local_nls,
  199. cifs_sb->mnt_cifs_flags &
  200. CIFS_MOUNT_MAP_SPECIAL_CHR);
  201. }
  202. } else {
  203. /* BB implement mode setting via Windows security
  204. descriptors e.g. */
  205. /* CIFSSMBWinSetPerms(xid,pTcon,path,mode,-1,-1,nls);*/
  206. /* Could set r/o dos attribute if mode & 0222 == 0 */
  207. }
  208. /* server might mask mode so we have to query for it */
  209. if (pTcon->unix_ext)
  210. rc = cifs_get_inode_info_unix(&newinode, full_path,
  211. inode->i_sb, xid);
  212. else {
  213. rc = cifs_get_inode_info(&newinode, full_path,
  214. buf, inode->i_sb, xid,
  215. &fileHandle);
  216. if (newinode) {
  217. newinode->i_mode = mode;
  218. if ((oplock & CIFS_CREATE_ACTION) &&
  219. (cifs_sb->mnt_cifs_flags &
  220. CIFS_MOUNT_SET_UID)) {
  221. newinode->i_uid = current->fsuid;
  222. newinode->i_gid = current->fsgid;
  223. }
  224. }
  225. }
  226. if (rc != 0) {
  227. cFYI(1,
  228. ("Create worked but get_inode_info failed rc = %d",
  229. rc));
  230. } else {
  231. if (pTcon->nocase)
  232. direntry->d_op = &cifs_ci_dentry_ops;
  233. else
  234. direntry->d_op = &cifs_dentry_ops;
  235. d_instantiate(direntry, newinode);
  236. }
  237. if ((nd == NULL /* nfsd case - nfs srv does not set nd */) ||
  238. ((nd->flags & LOOKUP_OPEN) == FALSE)) {
  239. /* mknod case - do not leave file open */
  240. CIFSSMBClose(xid, pTcon, fileHandle);
  241. } else if (newinode) {
  242. pCifsFile =
  243. kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  244. if (pCifsFile == NULL)
  245. goto cifs_create_out;
  246. pCifsFile->netfid = fileHandle;
  247. pCifsFile->pid = current->tgid;
  248. pCifsFile->pInode = newinode;
  249. pCifsFile->invalidHandle = FALSE;
  250. pCifsFile->closePend = FALSE;
  251. init_MUTEX(&pCifsFile->fh_sem);
  252. mutex_init(&pCifsFile->lock_mutex);
  253. INIT_LIST_HEAD(&pCifsFile->llist);
  254. atomic_set(&pCifsFile->wrtPending, 0);
  255. /* set the following in open now
  256. pCifsFile->pfile = file; */
  257. write_lock(&GlobalSMBSeslock);
  258. list_add(&pCifsFile->tlist, &pTcon->openFileList);
  259. pCifsInode = CIFS_I(newinode);
  260. if (pCifsInode) {
  261. /* if readable file instance put first in list*/
  262. if (write_only == TRUE) {
  263. list_add_tail(&pCifsFile->flist,
  264. &pCifsInode->openFileList);
  265. } else {
  266. list_add(&pCifsFile->flist,
  267. &pCifsInode->openFileList);
  268. }
  269. if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  270. pCifsInode->clientCanCacheAll = TRUE;
  271. pCifsInode->clientCanCacheRead = TRUE;
  272. cFYI(1, ("Exclusive Oplock inode %p",
  273. newinode));
  274. } else if ((oplock & 0xF) == OPLOCK_READ)
  275. pCifsInode->clientCanCacheRead = TRUE;
  276. }
  277. write_unlock(&GlobalSMBSeslock);
  278. }
  279. }
  280. cifs_create_out:
  281. kfree(buf);
  282. kfree(full_path);
  283. FreeXid(xid);
  284. return rc;
  285. }
  286. int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode,
  287. dev_t device_number)
  288. {
  289. int rc = -EPERM;
  290. int xid;
  291. struct cifs_sb_info *cifs_sb;
  292. struct cifsTconInfo *pTcon;
  293. char *full_path = NULL;
  294. struct inode *newinode = NULL;
  295. if (!old_valid_dev(device_number))
  296. return -EINVAL;
  297. xid = GetXid();
  298. cifs_sb = CIFS_SB(inode->i_sb);
  299. pTcon = cifs_sb->tcon;
  300. full_path = build_path_from_dentry(direntry);
  301. if (full_path == NULL)
  302. rc = -ENOMEM;
  303. else if (pTcon->unix_ext) {
  304. mode &= ~current->fs->umask;
  305. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  306. rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path,
  307. mode, (__u64)current->fsuid,
  308. (__u64)current->fsgid,
  309. device_number, cifs_sb->local_nls,
  310. cifs_sb->mnt_cifs_flags &
  311. CIFS_MOUNT_MAP_SPECIAL_CHR);
  312. } else {
  313. rc = CIFSSMBUnixSetPerms(xid, pTcon,
  314. full_path, mode, (__u64)-1, (__u64)-1,
  315. device_number, cifs_sb->local_nls,
  316. cifs_sb->mnt_cifs_flags &
  317. CIFS_MOUNT_MAP_SPECIAL_CHR);
  318. }
  319. if (!rc) {
  320. rc = cifs_get_inode_info_unix(&newinode, full_path,
  321. inode->i_sb, xid);
  322. if (pTcon->nocase)
  323. direntry->d_op = &cifs_ci_dentry_ops;
  324. else
  325. direntry->d_op = &cifs_dentry_ops;
  326. if (rc == 0)
  327. d_instantiate(direntry, newinode);
  328. }
  329. } else {
  330. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  331. int oplock = 0;
  332. u16 fileHandle;
  333. FILE_ALL_INFO *buf;
  334. cFYI(1, ("sfu compat create special file"));
  335. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  336. if (buf == NULL) {
  337. kfree(full_path);
  338. FreeXid(xid);
  339. return -ENOMEM;
  340. }
  341. rc = CIFSSMBOpen(xid, pTcon, full_path,
  342. FILE_CREATE, /* fail if exists */
  343. GENERIC_WRITE /* BB would
  344. WRITE_OWNER | WRITE_DAC be better? */,
  345. /* Create a file and set the
  346. file attribute to SYSTEM */
  347. CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
  348. &fileHandle, &oplock, buf,
  349. cifs_sb->local_nls,
  350. cifs_sb->mnt_cifs_flags &
  351. CIFS_MOUNT_MAP_SPECIAL_CHR);
  352. /* BB FIXME - add handling for backlevel servers
  353. which need legacy open and check for all
  354. calls to SMBOpen for fallback to SMBLeagcyOpen */
  355. if (!rc) {
  356. /* BB Do not bother to decode buf since no
  357. local inode yet to put timestamps in,
  358. but we can reuse it safely */
  359. unsigned int bytes_written;
  360. struct win_dev *pdev;
  361. pdev = (struct win_dev *)buf;
  362. if (S_ISCHR(mode)) {
  363. memcpy(pdev->type, "IntxCHR", 8);
  364. pdev->major =
  365. cpu_to_le64(MAJOR(device_number));
  366. pdev->minor =
  367. cpu_to_le64(MINOR(device_number));
  368. rc = CIFSSMBWrite(xid, pTcon,
  369. fileHandle,
  370. sizeof(struct win_dev),
  371. 0, &bytes_written, (char *)pdev,
  372. NULL, 0);
  373. } else if (S_ISBLK(mode)) {
  374. memcpy(pdev->type, "IntxBLK", 8);
  375. pdev->major =
  376. cpu_to_le64(MAJOR(device_number));
  377. pdev->minor =
  378. cpu_to_le64(MINOR(device_number));
  379. rc = CIFSSMBWrite(xid, pTcon,
  380. fileHandle,
  381. sizeof(struct win_dev),
  382. 0, &bytes_written, (char *)pdev,
  383. NULL, 0);
  384. } /* else if(S_ISFIFO */
  385. CIFSSMBClose(xid, pTcon, fileHandle);
  386. d_drop(direntry);
  387. }
  388. kfree(buf);
  389. /* add code here to set EAs */
  390. }
  391. }
  392. kfree(full_path);
  393. FreeXid(xid);
  394. return rc;
  395. }
  396. struct dentry *
  397. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
  398. struct nameidata *nd)
  399. {
  400. int xid;
  401. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  402. struct cifs_sb_info *cifs_sb;
  403. struct cifsTconInfo *pTcon;
  404. struct inode *newInode = NULL;
  405. char *full_path = NULL;
  406. xid = GetXid();
  407. cFYI(1, (" parent inode = 0x%p name is: %s and dentry = 0x%p",
  408. parent_dir_inode, direntry->d_name.name, direntry));
  409. /* check whether path exists */
  410. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  411. pTcon = cifs_sb->tcon;
  412. /*
  413. * Don't allow the separator character in a path component.
  414. * The VFS will not allow "/", but "\" is allowed by posix.
  415. */
  416. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
  417. int i;
  418. for (i = 0; i < direntry->d_name.len; i++)
  419. if (direntry->d_name.name[i] == '\\') {
  420. cFYI(1, ("Invalid file name"));
  421. FreeXid(xid);
  422. return ERR_PTR(-EINVAL);
  423. }
  424. }
  425. /* can not grab the rename sem here since it would
  426. deadlock in the cases (beginning of sys_rename itself)
  427. in which we already have the sb rename sem */
  428. full_path = build_path_from_dentry(direntry);
  429. if (full_path == NULL) {
  430. FreeXid(xid);
  431. return ERR_PTR(-ENOMEM);
  432. }
  433. if (direntry->d_inode != NULL) {
  434. cFYI(1, (" non-NULL inode in lookup"));
  435. } else {
  436. cFYI(1, (" NULL inode in lookup"));
  437. }
  438. cFYI(1,
  439. (" Full path: %s inode = 0x%p", full_path, direntry->d_inode));
  440. if (pTcon->unix_ext)
  441. rc = cifs_get_inode_info_unix(&newInode, full_path,
  442. parent_dir_inode->i_sb, xid);
  443. else
  444. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  445. parent_dir_inode->i_sb, xid, NULL);
  446. if ((rc == 0) && (newInode != NULL)) {
  447. if (pTcon->nocase)
  448. direntry->d_op = &cifs_ci_dentry_ops;
  449. else
  450. direntry->d_op = &cifs_dentry_ops;
  451. d_add(direntry, newInode);
  452. /* since paths are not looked up by component - the parent
  453. directories are presumed to be good here */
  454. renew_parental_timestamps(direntry);
  455. } else if (rc == -ENOENT) {
  456. rc = 0;
  457. direntry->d_time = jiffies;
  458. if (pTcon->nocase)
  459. direntry->d_op = &cifs_ci_dentry_ops;
  460. else
  461. direntry->d_op = &cifs_dentry_ops;
  462. d_add(direntry, NULL);
  463. /* if it was once a directory (but how can we tell?) we could do
  464. shrink_dcache_parent(direntry); */
  465. } else if (rc != -EACCES) {
  466. cERROR(1, ("Unexpected lookup error %d", rc));
  467. /* We special case check for Access Denied - since that
  468. is a common return code */
  469. }
  470. kfree(full_path);
  471. FreeXid(xid);
  472. return ERR_PTR(rc);
  473. }
  474. static int
  475. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  476. {
  477. int isValid = 1;
  478. if (direntry->d_inode) {
  479. if (cifs_revalidate(direntry))
  480. return 0;
  481. } else {
  482. cFYI(1, ("neg dentry 0x%p name = %s",
  483. direntry, direntry->d_name.name));
  484. if (time_after(jiffies, direntry->d_time + HZ) ||
  485. !lookupCacheEnabled) {
  486. d_drop(direntry);
  487. isValid = 0;
  488. }
  489. }
  490. return isValid;
  491. }
  492. /* static int cifs_d_delete(struct dentry *direntry)
  493. {
  494. int rc = 0;
  495. cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
  496. return rc;
  497. } */
  498. struct dentry_operations cifs_dentry_ops = {
  499. .d_revalidate = cifs_d_revalidate,
  500. /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
  501. };
  502. static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
  503. {
  504. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  505. unsigned long hash;
  506. int i;
  507. hash = init_name_hash();
  508. for (i = 0; i < q->len; i++)
  509. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  510. hash);
  511. q->hash = end_name_hash(hash);
  512. return 0;
  513. }
  514. static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
  515. struct qstr *b)
  516. {
  517. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  518. if ((a->len == b->len) &&
  519. (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
  520. /*
  521. * To preserve case, don't let an existing negative dentry's
  522. * case take precedence. If a is not a negative dentry, this
  523. * should have no side effects
  524. */
  525. memcpy(a->name, b->name, a->len);
  526. return 0;
  527. }
  528. return 1;
  529. }
  530. struct dentry_operations cifs_ci_dentry_ops = {
  531. .d_revalidate = cifs_d_revalidate,
  532. .d_hash = cifs_ci_hash,
  533. .d_compare = cifs_ci_compare,
  534. };