dir.c 16 KB

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