dir.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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, dev_t device_number)
  272. {
  273. int rc = -EPERM;
  274. int xid;
  275. struct cifs_sb_info *cifs_sb;
  276. struct cifsTconInfo *pTcon;
  277. char *full_path = NULL;
  278. struct inode * newinode = NULL;
  279. if (!old_valid_dev(device_number))
  280. return -EINVAL;
  281. xid = GetXid();
  282. cifs_sb = CIFS_SB(inode->i_sb);
  283. pTcon = cifs_sb->tcon;
  284. down(&direntry->d_sb->s_vfs_rename_sem);
  285. full_path = build_path_from_dentry(direntry);
  286. up(&direntry->d_sb->s_vfs_rename_sem);
  287. if(full_path == NULL)
  288. rc = -ENOMEM;
  289. else if (pTcon->ses->capabilities & CAP_UNIX) {
  290. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  291. rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path,
  292. mode,(__u64)current->euid,(__u64)current->egid,
  293. device_number, cifs_sb->local_nls,
  294. cifs_sb->mnt_cifs_flags &
  295. CIFS_MOUNT_MAP_SPECIAL_CHR);
  296. } else {
  297. rc = CIFSSMBUnixSetPerms(xid, pTcon,
  298. full_path, mode, (__u64)-1, (__u64)-1,
  299. device_number, cifs_sb->local_nls,
  300. cifs_sb->mnt_cifs_flags &
  301. CIFS_MOUNT_MAP_SPECIAL_CHR);
  302. }
  303. if(!rc) {
  304. rc = cifs_get_inode_info_unix(&newinode, full_path,
  305. inode->i_sb,xid);
  306. if (pTcon->nocase)
  307. direntry->d_op = &cifs_ci_dentry_ops;
  308. else
  309. direntry->d_op = &cifs_dentry_ops;
  310. if(rc == 0)
  311. d_instantiate(direntry, newinode);
  312. }
  313. } else {
  314. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  315. int oplock = 0;
  316. u16 fileHandle;
  317. FILE_ALL_INFO * buf;
  318. cFYI(1,("sfu compat create special file"));
  319. buf = kmalloc(sizeof(FILE_ALL_INFO),GFP_KERNEL);
  320. if(buf == NULL) {
  321. kfree(full_path);
  322. FreeXid(xid);
  323. return -ENOMEM;
  324. }
  325. rc = CIFSSMBOpen(xid, pTcon, full_path,
  326. FILE_CREATE, /* fail if exists */
  327. GENERIC_WRITE /* BB would
  328. WRITE_OWNER | WRITE_DAC be better? */,
  329. /* Create a file and set the
  330. file attribute to SYSTEM */
  331. CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
  332. &fileHandle, &oplock, buf,
  333. cifs_sb->local_nls,
  334. cifs_sb->mnt_cifs_flags &
  335. CIFS_MOUNT_MAP_SPECIAL_CHR);
  336. if(!rc) {
  337. /* BB Do not bother to decode buf since no
  338. local inode yet to put timestamps in */
  339. CIFSSMBClose(xid, pTcon, fileHandle);
  340. d_drop(direntry);
  341. }
  342. kfree(buf);
  343. /* add code here to set EAs */
  344. }
  345. }
  346. kfree(full_path);
  347. FreeXid(xid);
  348. return rc;
  349. }
  350. struct dentry *
  351. cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, struct nameidata *nd)
  352. {
  353. int xid;
  354. int rc = 0; /* to get around spurious gcc warning, set to zero here */
  355. struct cifs_sb_info *cifs_sb;
  356. struct cifsTconInfo *pTcon;
  357. struct inode *newInode = NULL;
  358. char *full_path = NULL;
  359. xid = GetXid();
  360. cFYI(1,
  361. (" parent inode = 0x%p name is: %s and dentry = 0x%p",
  362. parent_dir_inode, direntry->d_name.name, direntry));
  363. /* BB Add check of incoming data - e.g. frame not longer than maximum SMB - let server check the namelen BB */
  364. /* check whether path exists */
  365. cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
  366. pTcon = cifs_sb->tcon;
  367. /* can not grab the rename sem here since it would
  368. deadlock in the cases (beginning of sys_rename itself)
  369. in which we already have the sb rename sem */
  370. full_path = build_path_from_dentry(direntry);
  371. if(full_path == NULL) {
  372. FreeXid(xid);
  373. return ERR_PTR(-ENOMEM);
  374. }
  375. if (direntry->d_inode != NULL) {
  376. cFYI(1, (" non-NULL inode in lookup"));
  377. } else {
  378. cFYI(1, (" NULL inode in lookup"));
  379. }
  380. cFYI(1,
  381. (" Full path: %s inode = 0x%p", full_path, direntry->d_inode));
  382. if (pTcon->ses->capabilities & CAP_UNIX)
  383. rc = cifs_get_inode_info_unix(&newInode, full_path,
  384. parent_dir_inode->i_sb,xid);
  385. else
  386. rc = cifs_get_inode_info(&newInode, full_path, NULL,
  387. parent_dir_inode->i_sb,xid);
  388. if ((rc == 0) && (newInode != NULL)) {
  389. if (pTcon->nocase)
  390. direntry->d_op = &cifs_ci_dentry_ops;
  391. else
  392. direntry->d_op = &cifs_dentry_ops;
  393. d_add(direntry, newInode);
  394. /* since paths are not looked up by component - the parent directories are presumed to be good here */
  395. renew_parental_timestamps(direntry);
  396. } else if (rc == -ENOENT) {
  397. rc = 0;
  398. d_add(direntry, NULL);
  399. } else {
  400. cERROR(1,("Error 0x%x on cifs_get_inode_info in lookup of %s",
  401. rc,full_path));
  402. /* BB special case check for Access Denied - watch security
  403. exposure of returning dir info implicitly via different rc
  404. if file exists or not but no access BB */
  405. }
  406. kfree(full_path);
  407. FreeXid(xid);
  408. return ERR_PTR(rc);
  409. }
  410. static int
  411. cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
  412. {
  413. int isValid = 1;
  414. /* lock_kernel(); *//* surely we do not want to lock the kernel for a whole network round trip which could take seconds */
  415. if (direntry->d_inode) {
  416. if (cifs_revalidate(direntry)) {
  417. /* unlock_kernel(); */
  418. return 0;
  419. }
  420. } else {
  421. cFYI(1,
  422. ("In cifs_d_revalidate with no inode but name = %s and dentry 0x%p",
  423. direntry->d_name.name, direntry));
  424. }
  425. /* unlock_kernel(); */
  426. return isValid;
  427. }
  428. /* static int cifs_d_delete(struct dentry *direntry)
  429. {
  430. int rc = 0;
  431. cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
  432. return rc;
  433. } */
  434. struct dentry_operations cifs_dentry_ops = {
  435. .d_revalidate = cifs_d_revalidate,
  436. /* d_delete: cifs_d_delete, *//* not needed except for debugging */
  437. /* no need for d_hash, d_compare, d_release, d_iput ... yet. BB confirm this BB */
  438. };
  439. static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
  440. {
  441. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  442. unsigned long hash;
  443. int i;
  444. hash = init_name_hash();
  445. for (i = 0; i < q->len; i++)
  446. hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
  447. hash);
  448. q->hash = end_name_hash(hash);
  449. return 0;
  450. }
  451. static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
  452. struct qstr *b)
  453. {
  454. struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
  455. if ((a->len == b->len) &&
  456. (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
  457. /*
  458. * To preserve case, don't let an existing negative dentry's
  459. * case take precedence. If a is not a negative dentry, this
  460. * should have no side effects
  461. */
  462. memcpy((unsigned char *)a->name, b->name, a->len);
  463. return 0;
  464. }
  465. return 1;
  466. }
  467. struct dentry_operations cifs_ci_dentry_ops = {
  468. .d_revalidate = cifs_d_revalidate,
  469. .d_hash = cifs_ci_hash,
  470. .d_compare = cifs_ci_compare,
  471. };