dir.c 15 KB

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