dir.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. if (newinode) {
  216. newinode->i_mode = mode;
  217. if ((oplock & CIFS_CREATE_ACTION) &&
  218. (cifs_sb->mnt_cifs_flags &
  219. CIFS_MOUNT_SET_UID)) {
  220. newinode->i_uid = current->fsuid;
  221. newinode->i_gid = current->fsgid;
  222. }
  223. }
  224. }
  225. if (rc != 0) {
  226. cFYI(1,
  227. ("Create worked but get_inode_info failed rc = %d",
  228. rc));
  229. } else {
  230. if (pTcon->nocase)
  231. direntry->d_op = &cifs_ci_dentry_ops;
  232. else
  233. direntry->d_op = &cifs_dentry_ops;
  234. d_instantiate(direntry, newinode);
  235. }
  236. if ((nd == NULL /* nfsd case - nfs srv does not set nd */) ||
  237. ((nd->flags & LOOKUP_OPEN) == FALSE)) {
  238. /* mknod case - do not leave file open */
  239. CIFSSMBClose(xid, pTcon, fileHandle);
  240. } else if (newinode) {
  241. pCifsFile =
  242. kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  243. if (pCifsFile == NULL)
  244. goto cifs_create_out;
  245. pCifsFile->netfid = fileHandle;
  246. pCifsFile->pid = current->tgid;
  247. pCifsFile->pInode = newinode;
  248. pCifsFile->invalidHandle = FALSE;
  249. pCifsFile->closePend = FALSE;
  250. init_MUTEX(&pCifsFile->fh_sem);
  251. mutex_init(&pCifsFile->lock_mutex);
  252. INIT_LIST_HEAD(&pCifsFile->llist);
  253. atomic_set(&pCifsFile->wrtPending, 0);
  254. /* set the following in open now
  255. pCifsFile->pfile = file; */
  256. write_lock(&GlobalSMBSeslock);
  257. list_add(&pCifsFile->tlist, &pTcon->openFileList);
  258. pCifsInode = CIFS_I(newinode);
  259. if (pCifsInode) {
  260. /* if readable file instance put first in list*/
  261. if (write_only == TRUE) {
  262. list_add_tail(&pCifsFile->flist,
  263. &pCifsInode->openFileList);
  264. } else {
  265. list_add(&pCifsFile->flist,
  266. &pCifsInode->openFileList);
  267. }
  268. if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  269. pCifsInode->clientCanCacheAll = TRUE;
  270. pCifsInode->clientCanCacheRead = TRUE;
  271. cFYI(1, ("Exclusive Oplock inode %p",
  272. newinode));
  273. } else if ((oplock & 0xF) == OPLOCK_READ)
  274. pCifsInode->clientCanCacheRead = TRUE;
  275. }
  276. write_unlock(&GlobalSMBSeslock);
  277. }
  278. }
  279. cifs_create_out:
  280. kfree(buf);
  281. kfree(full_path);
  282. FreeXid(xid);
  283. return rc;
  284. }
  285. int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode,
  286. dev_t device_number)
  287. {
  288. int rc = -EPERM;
  289. int xid;
  290. struct cifs_sb_info *cifs_sb;
  291. struct cifsTconInfo *pTcon;
  292. char *full_path = NULL;
  293. struct inode *newinode = NULL;
  294. if (!old_valid_dev(device_number))
  295. return -EINVAL;
  296. xid = GetXid();
  297. cifs_sb = CIFS_SB(inode->i_sb);
  298. pTcon = cifs_sb->tcon;
  299. full_path = build_path_from_dentry(direntry);
  300. if (full_path == NULL)
  301. rc = -ENOMEM;
  302. else if (pTcon->unix_ext) {
  303. mode &= ~current->fs->umask;
  304. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  305. rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path,
  306. mode, (__u64)current->fsuid,
  307. (__u64)current->fsgid,
  308. device_number, cifs_sb->local_nls,
  309. cifs_sb->mnt_cifs_flags &
  310. CIFS_MOUNT_MAP_SPECIAL_CHR);
  311. } else {
  312. rc = CIFSSMBUnixSetPerms(xid, pTcon,
  313. full_path, mode, (__u64)-1, (__u64)-1,
  314. device_number, cifs_sb->local_nls,
  315. cifs_sb->mnt_cifs_flags &
  316. CIFS_MOUNT_MAP_SPECIAL_CHR);
  317. }
  318. if (!rc) {
  319. rc = cifs_get_inode_info_unix(&newinode, full_path,
  320. inode->i_sb, xid);
  321. if (pTcon->nocase)
  322. direntry->d_op = &cifs_ci_dentry_ops;
  323. else
  324. direntry->d_op = &cifs_dentry_ops;
  325. if (rc == 0)
  326. d_instantiate(direntry, newinode);
  327. }
  328. } else {
  329. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  330. int oplock = 0;
  331. u16 fileHandle;
  332. FILE_ALL_INFO *buf;
  333. cFYI(1, ("sfu compat create special file"));
  334. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  335. if (buf == NULL) {
  336. kfree(full_path);
  337. FreeXid(xid);
  338. return -ENOMEM;
  339. }
  340. rc = CIFSSMBOpen(xid, pTcon, full_path,
  341. FILE_CREATE, /* fail if exists */
  342. GENERIC_WRITE /* BB would
  343. WRITE_OWNER | WRITE_DAC be better? */,
  344. /* Create a file and set the
  345. file attribute to SYSTEM */
  346. CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
  347. &fileHandle, &oplock, buf,
  348. cifs_sb->local_nls,
  349. cifs_sb->mnt_cifs_flags &
  350. CIFS_MOUNT_MAP_SPECIAL_CHR);
  351. /* BB FIXME - add handling for backlevel servers
  352. which need legacy open and check for all
  353. calls to SMBOpen for fallback to SMBLeagcyOpen */
  354. if (!rc) {
  355. /* BB Do not bother to decode buf since no
  356. local inode yet to put timestamps in,
  357. but we can reuse it safely */
  358. unsigned int bytes_written;
  359. struct win_dev *pdev;
  360. pdev = (struct win_dev *)buf;
  361. if (S_ISCHR(mode)) {
  362. memcpy(pdev->type, "IntxCHR", 8);
  363. pdev->major =
  364. cpu_to_le64(MAJOR(device_number));
  365. pdev->minor =
  366. cpu_to_le64(MINOR(device_number));
  367. rc = CIFSSMBWrite(xid, pTcon,
  368. fileHandle,
  369. sizeof(struct win_dev),
  370. 0, &bytes_written, (char *)pdev,
  371. NULL, 0);
  372. } else if (S_ISBLK(mode)) {
  373. memcpy(pdev->type, "IntxBLK", 8);
  374. pdev->major =
  375. cpu_to_le64(MAJOR(device_number));
  376. pdev->minor =
  377. cpu_to_le64(MINOR(device_number));
  378. rc = CIFSSMBWrite(xid, pTcon,
  379. fileHandle,
  380. sizeof(struct win_dev),
  381. 0, &bytes_written, (char *)pdev,
  382. NULL, 0);
  383. } /* else if(S_ISFIFO */
  384. CIFSSMBClose(xid, pTcon, fileHandle);
  385. d_drop(direntry);
  386. }
  387. kfree(buf);
  388. /* add code here to set EAs */
  389. }
  390. }
  391. kfree(full_path);
  392. FreeXid(xid);
  393. return rc;
  394. }
  395. struct dentry *
  396. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
  397. struct nameidata *nd)
  398. {
  399. int xid;
  400. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  401. struct cifs_sb_info *cifs_sb;
  402. struct cifsTconInfo *pTcon;
  403. struct inode *newInode = NULL;
  404. char *full_path = NULL;
  405. xid = GetXid();
  406. cFYI(1, (" parent inode = 0x%p name is: %s and dentry = 0x%p",
  407. parent_dir_inode, direntry->d_name.name, direntry));
  408. /* check whether path exists */
  409. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  410. pTcon = cifs_sb->tcon;
  411. /*
  412. * Don't allow the separator character in a path component.
  413. * The VFS will not allow "/", but "\" is allowed by posix.
  414. */
  415. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
  416. int i;
  417. for (i = 0; i < direntry->d_name.len; i++)
  418. if (direntry->d_name.name[i] == '\\') {
  419. cFYI(1, ("Invalid file name"));
  420. FreeXid(xid);
  421. return ERR_PTR(-EINVAL);
  422. }
  423. }
  424. /* can not grab the rename sem here since it would
  425. deadlock in the cases (beginning of sys_rename itself)
  426. in which we already have the sb rename sem */
  427. full_path = build_path_from_dentry(direntry);
  428. if (full_path == NULL) {
  429. FreeXid(xid);
  430. return ERR_PTR(-ENOMEM);
  431. }
  432. if (direntry->d_inode != NULL) {
  433. cFYI(1, (" non-NULL inode in lookup"));
  434. } else {
  435. cFYI(1, (" NULL inode in lookup"));
  436. }
  437. cFYI(1,
  438. (" Full path: %s inode = 0x%p", full_path, direntry->d_inode));
  439. if (pTcon->unix_ext)
  440. rc = cifs_get_inode_info_unix(&newInode, full_path,
  441. parent_dir_inode->i_sb, xid);
  442. else
  443. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  444. parent_dir_inode->i_sb, xid);
  445. if ((rc == 0) && (newInode != NULL)) {
  446. if (pTcon->nocase)
  447. direntry->d_op = &cifs_ci_dentry_ops;
  448. else
  449. direntry->d_op = &cifs_dentry_ops;
  450. d_add(direntry, newInode);
  451. /* since paths are not looked up by component - the parent
  452. directories are presumed to be good here */
  453. renew_parental_timestamps(direntry);
  454. } else if (rc == -ENOENT) {
  455. rc = 0;
  456. direntry->d_time = jiffies;
  457. if (pTcon->nocase)
  458. direntry->d_op = &cifs_ci_dentry_ops;
  459. else
  460. direntry->d_op = &cifs_dentry_ops;
  461. d_add(direntry, NULL);
  462. /* if it was once a directory (but how can we tell?) we could do
  463. shrink_dcache_parent(direntry); */
  464. } else if (rc != -EACCES) {
  465. cERROR(1, ("Unexpected lookup error %d", rc));
  466. /* We special case check for Access Denied - since that
  467. is a common return code */
  468. }
  469. kfree(full_path);
  470. FreeXid(xid);
  471. return ERR_PTR(rc);
  472. }
  473. static int
  474. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  475. {
  476. int isValid = 1;
  477. if (direntry->d_inode) {
  478. if (cifs_revalidate(direntry))
  479. return 0;
  480. } else {
  481. cFYI(1, ("neg dentry 0x%p name = %s",
  482. direntry, direntry->d_name.name));
  483. if (time_after(jiffies, direntry->d_time + HZ) ||
  484. !lookupCacheEnabled) {
  485. d_drop(direntry);
  486. isValid = 0;
  487. }
  488. }
  489. return isValid;
  490. }
  491. /* static int cifs_d_delete(struct dentry *direntry)
  492. {
  493. int rc = 0;
  494. cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
  495. return rc;
  496. } */
  497. struct dentry_operations cifs_dentry_ops = {
  498. .d_revalidate = cifs_d_revalidate,
  499. /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
  500. };
  501. static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
  502. {
  503. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  504. unsigned long hash;
  505. int i;
  506. hash = init_name_hash();
  507. for (i = 0; i < q->len; i++)
  508. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  509. hash);
  510. q->hash = end_name_hash(hash);
  511. return 0;
  512. }
  513. static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
  514. struct qstr *b)
  515. {
  516. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  517. if ((a->len == b->len) &&
  518. (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
  519. /*
  520. * To preserve case, don't let an existing negative dentry's
  521. * case take precedence. If a is not a negative dentry, this
  522. * should have no side effects
  523. */
  524. memcpy(a->name, b->name, a->len);
  525. return 0;
  526. }
  527. return 1;
  528. }
  529. struct dentry_operations cifs_ci_dentry_ops = {
  530. .d_revalidate = cifs_d_revalidate,
  531. .d_hash = cifs_ci_hash,
  532. .d_compare = cifs_ci_compare,
  533. };