dir.c 16 KB

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