dir.c 16 KB

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