dir.c 14 KB

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