readdir.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /*
  2. * fs/cifs/readdir.c
  3. *
  4. * Directory search handling
  5. *
  6. * Copyright (C) International Business Machines Corp., 2004, 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/smp_lock.h>
  26. #include "cifspdu.h"
  27. #include "cifsglob.h"
  28. #include "cifsproto.h"
  29. #include "cifs_unicode.h"
  30. #include "cifs_debug.h"
  31. #include "cifs_fs_sb.h"
  32. #include "cifsfs.h"
  33. #ifdef CONFIG_CIFS_DEBUG2
  34. static void dump_cifs_file_struct(struct file *file, char *label)
  35. {
  36. struct cifsFileInfo * cf;
  37. if(file) {
  38. cf = file->private_data;
  39. if(cf == NULL) {
  40. cFYI(1,("empty cifs private file data"));
  41. return;
  42. }
  43. if(cf->invalidHandle) {
  44. cFYI(1,("invalid handle"));
  45. }
  46. if(cf->srch_inf.endOfSearch) {
  47. cFYI(1,("end of search"));
  48. }
  49. if(cf->srch_inf.emptyDir) {
  50. cFYI(1,("empty dir"));
  51. }
  52. }
  53. }
  54. #endif /* DEBUG2 */
  55. /* Returns one if new inode created (which therefore needs to be hashed) */
  56. /* Might check in the future if inode number changed so we can rehash inode */
  57. static int construct_dentry(struct qstr *qstring, struct file *file,
  58. struct inode **ptmp_inode, struct dentry **pnew_dentry)
  59. {
  60. struct dentry *tmp_dentry;
  61. struct cifs_sb_info *cifs_sb;
  62. struct cifsTconInfo *pTcon;
  63. int rc = 0;
  64. cFYI(1, ("For %s", qstring->name));
  65. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  66. pTcon = cifs_sb->tcon;
  67. qstring->hash = full_name_hash(qstring->name, qstring->len);
  68. tmp_dentry = d_lookup(file->f_dentry, qstring);
  69. if (tmp_dentry) {
  70. cFYI(0, ("existing dentry with inode 0x%p", tmp_dentry->d_inode));
  71. *ptmp_inode = tmp_dentry->d_inode;
  72. /* BB overwrite old name? i.e. tmp_dentry->d_name and tmp_dentry->d_name.len??*/
  73. if(*ptmp_inode == NULL) {
  74. *ptmp_inode = new_inode(file->f_dentry->d_sb);
  75. if(*ptmp_inode == NULL)
  76. return rc;
  77. rc = 1;
  78. d_instantiate(tmp_dentry, *ptmp_inode);
  79. }
  80. } else {
  81. tmp_dentry = d_alloc(file->f_dentry, qstring);
  82. if(tmp_dentry == NULL) {
  83. cERROR(1,("Failed allocating dentry"));
  84. *ptmp_inode = NULL;
  85. return rc;
  86. }
  87. *ptmp_inode = new_inode(file->f_dentry->d_sb);
  88. if (pTcon->nocase)
  89. tmp_dentry->d_op = &cifs_ci_dentry_ops;
  90. else
  91. tmp_dentry->d_op = &cifs_dentry_ops;
  92. if(*ptmp_inode == NULL)
  93. return rc;
  94. rc = 1;
  95. d_instantiate(tmp_dentry, *ptmp_inode);
  96. d_rehash(tmp_dentry);
  97. }
  98. tmp_dentry->d_time = jiffies;
  99. *pnew_dentry = tmp_dentry;
  100. return rc;
  101. }
  102. static void fill_in_inode(struct inode *tmp_inode,
  103. FILE_DIRECTORY_INFO *pfindData, int *pobject_type, int isNewInode)
  104. {
  105. loff_t local_size;
  106. struct timespec local_mtime;
  107. struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
  108. struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
  109. __u32 attr = le32_to_cpu(pfindData->ExtFileAttributes);
  110. __u64 allocation_size = le64_to_cpu(pfindData->AllocationSize);
  111. __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
  112. cifsInfo->cifsAttrs = attr;
  113. cifsInfo->time = jiffies;
  114. /* save mtime and size */
  115. local_mtime = tmp_inode->i_mtime;
  116. local_size = tmp_inode->i_size;
  117. /* Linux can not store file creation time unfortunately so ignore it */
  118. tmp_inode->i_atime =
  119. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
  120. tmp_inode->i_mtime =
  121. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
  122. tmp_inode->i_ctime =
  123. cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
  124. /* treat dos attribute of read-only as read-only mode bit e.g. 555? */
  125. /* 2767 perms - indicate mandatory locking */
  126. /* BB fill in uid and gid here? with help from winbind?
  127. or retrieve from NTFS stream extended attribute */
  128. if (atomic_read(&cifsInfo->inUse) == 0) {
  129. tmp_inode->i_uid = cifs_sb->mnt_uid;
  130. tmp_inode->i_gid = cifs_sb->mnt_gid;
  131. /* set default mode. will override for dirs below */
  132. tmp_inode->i_mode = cifs_sb->mnt_file_mode;
  133. } else {
  134. /* mask off the type bits since it gets set
  135. below and we do not want to get two type
  136. bits set */
  137. tmp_inode->i_mode &= ~S_IFMT;
  138. }
  139. if (attr & ATTR_DIRECTORY) {
  140. *pobject_type = DT_DIR;
  141. /* override default perms since we do not lock dirs */
  142. if(atomic_read(&cifsInfo->inUse) == 0) {
  143. tmp_inode->i_mode = cifs_sb->mnt_dir_mode;
  144. }
  145. tmp_inode->i_mode |= S_IFDIR;
  146. } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
  147. (attr & ATTR_SYSTEM)) {
  148. if (end_of_file == 0) {
  149. *pobject_type = DT_FIFO;
  150. tmp_inode->i_mode |= S_IFIFO;
  151. } else {
  152. /* rather than get the type here, we mark the
  153. inode as needing revalidate and get the real type
  154. (blk vs chr vs. symlink) later ie in lookup */
  155. *pobject_type = DT_REG;
  156. tmp_inode->i_mode |= S_IFREG;
  157. cifsInfo->time = 0;
  158. }
  159. /* we no longer mark these because we could not follow them */
  160. /* } else if (attr & ATTR_REPARSE) {
  161. *pobject_type = DT_LNK;
  162. tmp_inode->i_mode |= S_IFLNK; */
  163. } else {
  164. *pobject_type = DT_REG;
  165. tmp_inode->i_mode |= S_IFREG;
  166. if (attr & ATTR_READONLY)
  167. tmp_inode->i_mode &= ~(S_IWUGO);
  168. } /* could add code here - to validate if device or weird share type? */
  169. /* can not fill in nlink here as in qpathinfo version and Unx search */
  170. if (atomic_read(&cifsInfo->inUse) == 0) {
  171. atomic_set(&cifsInfo->inUse, 1);
  172. }
  173. if (is_size_safe_to_change(cifsInfo)) {
  174. /* can not safely change the file size here if the
  175. client is writing to it due to potential races */
  176. i_size_write(tmp_inode, end_of_file);
  177. /* 512 bytes (2**9) is the fake blocksize that must be used */
  178. /* for this calculation, even though the reported blocksize is larger */
  179. tmp_inode->i_blocks = (512 - 1 + allocation_size) >> 9;
  180. }
  181. if (allocation_size < end_of_file)
  182. cFYI(1, ("May be sparse file, allocation less than file size"));
  183. cFYI(1, ("File Size %ld and blocks %llu and blocksize %ld",
  184. (unsigned long)tmp_inode->i_size,
  185. (unsigned long long)tmp_inode->i_blocks,
  186. tmp_inode->i_blksize));
  187. if (S_ISREG(tmp_inode->i_mode)) {
  188. cFYI(1, ("File inode"));
  189. tmp_inode->i_op = &cifs_file_inode_ops;
  190. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  191. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  192. tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
  193. else
  194. tmp_inode->i_fop = &cifs_file_direct_ops;
  195. } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  196. tmp_inode->i_fop = &cifs_file_nobrl_ops;
  197. else
  198. tmp_inode->i_fop = &cifs_file_ops;
  199. tmp_inode->i_data.a_ops = &cifs_addr_ops;
  200. if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
  201. (cifs_sb->tcon->ses->server->maxBuf <
  202. 4096 + MAX_CIFS_HDR_SIZE))
  203. tmp_inode->i_data.a_ops->readpages = NULL;
  204. if(isNewInode)
  205. return; /* No sense invalidating pages for new inode
  206. since have not started caching readahead file
  207. data yet */
  208. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  209. (local_size == tmp_inode->i_size)) {
  210. cFYI(1, ("inode exists but unchanged"));
  211. } else {
  212. /* file may have changed on server */
  213. cFYI(1, ("invalidate inode, readdir detected change"));
  214. invalidate_remote_inode(tmp_inode);
  215. }
  216. } else if (S_ISDIR(tmp_inode->i_mode)) {
  217. cFYI(1, ("Directory inode"));
  218. tmp_inode->i_op = &cifs_dir_inode_ops;
  219. tmp_inode->i_fop = &cifs_dir_ops;
  220. } else if (S_ISLNK(tmp_inode->i_mode)) {
  221. cFYI(1, ("Symbolic Link inode"));
  222. tmp_inode->i_op = &cifs_symlink_inode_ops;
  223. } else {
  224. cFYI(1, ("Init special inode"));
  225. init_special_inode(tmp_inode, tmp_inode->i_mode,
  226. tmp_inode->i_rdev);
  227. }
  228. }
  229. static void unix_fill_in_inode(struct inode *tmp_inode,
  230. FILE_UNIX_INFO *pfindData, int *pobject_type, int isNewInode)
  231. {
  232. loff_t local_size;
  233. struct timespec local_mtime;
  234. struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
  235. struct cifs_sb_info *cifs_sb = CIFS_SB(tmp_inode->i_sb);
  236. __u32 type = le32_to_cpu(pfindData->Type);
  237. __u64 num_of_bytes = le64_to_cpu(pfindData->NumOfBytes);
  238. __u64 end_of_file = le64_to_cpu(pfindData->EndOfFile);
  239. cifsInfo->time = jiffies;
  240. atomic_inc(&cifsInfo->inUse);
  241. /* save mtime and size */
  242. local_mtime = tmp_inode->i_mtime;
  243. local_size = tmp_inode->i_size;
  244. tmp_inode->i_atime =
  245. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
  246. tmp_inode->i_mtime =
  247. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastModificationTime));
  248. tmp_inode->i_ctime =
  249. cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastStatusChange));
  250. tmp_inode->i_mode = le64_to_cpu(pfindData->Permissions);
  251. /* since we set the inode type below we need to mask off type
  252. to avoid strange results if bits above were corrupt */
  253. tmp_inode->i_mode &= ~S_IFMT;
  254. if (type == UNIX_FILE) {
  255. *pobject_type = DT_REG;
  256. tmp_inode->i_mode |= S_IFREG;
  257. } else if (type == UNIX_SYMLINK) {
  258. *pobject_type = DT_LNK;
  259. tmp_inode->i_mode |= S_IFLNK;
  260. } else if (type == UNIX_DIR) {
  261. *pobject_type = DT_DIR;
  262. tmp_inode->i_mode |= S_IFDIR;
  263. } else if (type == UNIX_CHARDEV) {
  264. *pobject_type = DT_CHR;
  265. tmp_inode->i_mode |= S_IFCHR;
  266. tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
  267. le64_to_cpu(pfindData->DevMinor) & MINORMASK);
  268. } else if (type == UNIX_BLOCKDEV) {
  269. *pobject_type = DT_BLK;
  270. tmp_inode->i_mode |= S_IFBLK;
  271. tmp_inode->i_rdev = MKDEV(le64_to_cpu(pfindData->DevMajor),
  272. le64_to_cpu(pfindData->DevMinor) & MINORMASK);
  273. } else if (type == UNIX_FIFO) {
  274. *pobject_type = DT_FIFO;
  275. tmp_inode->i_mode |= S_IFIFO;
  276. } else if (type == UNIX_SOCKET) {
  277. *pobject_type = DT_SOCK;
  278. tmp_inode->i_mode |= S_IFSOCK;
  279. } else {
  280. /* safest to just call it a file */
  281. *pobject_type = DT_REG;
  282. tmp_inode->i_mode |= S_IFREG;
  283. cFYI(1,("unknown inode type %d",type));
  284. }
  285. tmp_inode->i_uid = le64_to_cpu(pfindData->Uid);
  286. tmp_inode->i_gid = le64_to_cpu(pfindData->Gid);
  287. tmp_inode->i_nlink = le64_to_cpu(pfindData->Nlinks);
  288. if (is_size_safe_to_change(cifsInfo)) {
  289. /* can not safely change the file size here if the
  290. client is writing to it due to potential races */
  291. i_size_write(tmp_inode,end_of_file);
  292. /* 512 bytes (2**9) is the fake blocksize that must be used */
  293. /* for this calculation, not the real blocksize */
  294. tmp_inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
  295. }
  296. if (S_ISREG(tmp_inode->i_mode)) {
  297. cFYI(1, ("File inode"));
  298. tmp_inode->i_op = &cifs_file_inode_ops;
  299. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  300. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  301. tmp_inode->i_fop = &cifs_file_direct_nobrl_ops;
  302. else
  303. tmp_inode->i_fop = &cifs_file_direct_ops;
  304. } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  305. tmp_inode->i_fop = &cifs_file_nobrl_ops;
  306. else
  307. tmp_inode->i_fop = &cifs_file_ops;
  308. tmp_inode->i_data.a_ops = &cifs_addr_ops;
  309. if((cifs_sb->tcon) && (cifs_sb->tcon->ses) &&
  310. (cifs_sb->tcon->ses->server->maxBuf <
  311. 4096 + MAX_CIFS_HDR_SIZE))
  312. tmp_inode->i_data.a_ops->readpages = NULL;
  313. if(isNewInode)
  314. return; /* No sense invalidating pages for new inode since we
  315. have not started caching readahead file data yet */
  316. if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
  317. (local_size == tmp_inode->i_size)) {
  318. cFYI(1, ("inode exists but unchanged"));
  319. } else {
  320. /* file may have changed on server */
  321. cFYI(1, ("invalidate inode, readdir detected change"));
  322. invalidate_remote_inode(tmp_inode);
  323. }
  324. } else if (S_ISDIR(tmp_inode->i_mode)) {
  325. cFYI(1, ("Directory inode"));
  326. tmp_inode->i_op = &cifs_dir_inode_ops;
  327. tmp_inode->i_fop = &cifs_dir_ops;
  328. } else if (S_ISLNK(tmp_inode->i_mode)) {
  329. cFYI(1, ("Symbolic Link inode"));
  330. tmp_inode->i_op = &cifs_symlink_inode_ops;
  331. /* tmp_inode->i_fop = *//* do not need to set to anything */
  332. } else {
  333. cFYI(1, ("Special inode"));
  334. init_special_inode(tmp_inode, tmp_inode->i_mode,
  335. tmp_inode->i_rdev);
  336. }
  337. }
  338. static int initiate_cifs_search(const int xid, struct file *file)
  339. {
  340. int rc = 0;
  341. char * full_path;
  342. struct cifsFileInfo * cifsFile;
  343. struct cifs_sb_info *cifs_sb;
  344. struct cifsTconInfo *pTcon;
  345. if(file->private_data == NULL) {
  346. file->private_data =
  347. kmalloc(sizeof(struct cifsFileInfo),GFP_KERNEL);
  348. }
  349. if(file->private_data == NULL) {
  350. return -ENOMEM;
  351. } else {
  352. memset(file->private_data,0,sizeof(struct cifsFileInfo));
  353. }
  354. cifsFile = file->private_data;
  355. cifsFile->invalidHandle = TRUE;
  356. cifsFile->srch_inf.endOfSearch = FALSE;
  357. if(file->f_dentry == NULL)
  358. return -ENOENT;
  359. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  360. if(cifs_sb == NULL)
  361. return -EINVAL;
  362. pTcon = cifs_sb->tcon;
  363. if(pTcon == NULL)
  364. return -EINVAL;
  365. full_path = build_path_from_dentry(file->f_dentry);
  366. if(full_path == NULL) {
  367. return -ENOMEM;
  368. }
  369. cFYI(1, ("Full path: %s start at: %lld", full_path, file->f_pos));
  370. ffirst_retry:
  371. /* test for Unix extensions */
  372. if (pTcon->ses->capabilities & CAP_UNIX) {
  373. cifsFile->srch_inf.info_level = SMB_FIND_FILE_UNIX;
  374. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
  375. cifsFile->srch_inf.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
  376. } else /* not srvinos - BB fixme add check for backlevel? */ {
  377. cifsFile->srch_inf.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
  378. }
  379. rc = CIFSFindFirst(xid, pTcon,full_path,cifs_sb->local_nls,
  380. &cifsFile->netfid, &cifsFile->srch_inf,
  381. cifs_sb->mnt_cifs_flags &
  382. CIFS_MOUNT_MAP_SPECIAL_CHR, CIFS_DIR_SEP(cifs_sb));
  383. if(rc == 0)
  384. cifsFile->invalidHandle = FALSE;
  385. if((rc == -EOPNOTSUPP) &&
  386. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
  387. cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_SERVER_INUM;
  388. goto ffirst_retry;
  389. }
  390. kfree(full_path);
  391. return rc;
  392. }
  393. /* return length of unicode string in bytes */
  394. static int cifs_unicode_bytelen(char *str)
  395. {
  396. int len;
  397. __le16 * ustr = (__le16 *)str;
  398. for(len=0;len <= PATH_MAX;len++) {
  399. if(ustr[len] == 0)
  400. return len << 1;
  401. }
  402. cFYI(1,("Unicode string longer than PATH_MAX found"));
  403. return len << 1;
  404. }
  405. static char *nxt_dir_entry(char *old_entry, char *end_of_smb)
  406. {
  407. char * new_entry;
  408. FILE_DIRECTORY_INFO * pDirInfo = (FILE_DIRECTORY_INFO *)old_entry;
  409. new_entry = old_entry + le32_to_cpu(pDirInfo->NextEntryOffset);
  410. cFYI(1,("new entry %p old entry %p",new_entry,old_entry));
  411. /* validate that new_entry is not past end of SMB */
  412. if(new_entry >= end_of_smb) {
  413. cERROR(1,
  414. ("search entry %p began after end of SMB %p old entry %p",
  415. new_entry, end_of_smb, old_entry));
  416. return NULL;
  417. } else if (new_entry + sizeof(FILE_DIRECTORY_INFO) > end_of_smb) {
  418. cERROR(1,("search entry %p extends after end of SMB %p",
  419. new_entry, end_of_smb));
  420. return NULL;
  421. } else
  422. return new_entry;
  423. }
  424. #define UNICODE_DOT cpu_to_le16(0x2e)
  425. /* return 0 if no match and 1 for . (current directory) and 2 for .. (parent) */
  426. static int cifs_entry_is_dot(char *current_entry, struct cifsFileInfo *cfile)
  427. {
  428. int rc = 0;
  429. char * filename = NULL;
  430. int len = 0;
  431. if(cfile->srch_inf.info_level == 0x202) {
  432. FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
  433. filename = &pFindData->FileName[0];
  434. if(cfile->srch_inf.unicode) {
  435. len = cifs_unicode_bytelen(filename);
  436. } else {
  437. /* BB should we make this strnlen of PATH_MAX? */
  438. len = strnlen(filename, 5);
  439. }
  440. } else if(cfile->srch_inf.info_level == 0x101) {
  441. FILE_DIRECTORY_INFO * pFindData =
  442. (FILE_DIRECTORY_INFO *)current_entry;
  443. filename = &pFindData->FileName[0];
  444. len = le32_to_cpu(pFindData->FileNameLength);
  445. } else if(cfile->srch_inf.info_level == 0x102) {
  446. FILE_FULL_DIRECTORY_INFO * pFindData =
  447. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  448. filename = &pFindData->FileName[0];
  449. len = le32_to_cpu(pFindData->FileNameLength);
  450. } else if(cfile->srch_inf.info_level == 0x105) {
  451. SEARCH_ID_FULL_DIR_INFO * pFindData =
  452. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  453. filename = &pFindData->FileName[0];
  454. len = le32_to_cpu(pFindData->FileNameLength);
  455. } else if(cfile->srch_inf.info_level == 0x104) {
  456. FILE_BOTH_DIRECTORY_INFO * pFindData =
  457. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  458. filename = &pFindData->FileName[0];
  459. len = le32_to_cpu(pFindData->FileNameLength);
  460. } else {
  461. cFYI(1,("Unknown findfirst level %d",cfile->srch_inf.info_level));
  462. }
  463. if(filename) {
  464. if(cfile->srch_inf.unicode) {
  465. __le16 *ufilename = (__le16 *)filename;
  466. if(len == 2) {
  467. /* check for . */
  468. if(ufilename[0] == UNICODE_DOT)
  469. rc = 1;
  470. } else if(len == 4) {
  471. /* check for .. */
  472. if((ufilename[0] == UNICODE_DOT)
  473. &&(ufilename[1] == UNICODE_DOT))
  474. rc = 2;
  475. }
  476. } else /* ASCII */ {
  477. if(len == 1) {
  478. if(filename[0] == '.')
  479. rc = 1;
  480. } else if(len == 2) {
  481. if((filename[0] == '.') && (filename[1] == '.'))
  482. rc = 2;
  483. }
  484. }
  485. }
  486. return rc;
  487. }
  488. /* Check if directory that we are searching has changed so we can decide
  489. whether we can use the cached search results from the previous search */
  490. static int is_dir_changed(struct file * file)
  491. {
  492. struct inode * inode;
  493. struct cifsInodeInfo *cifsInfo;
  494. if(file->f_dentry == NULL)
  495. return 0;
  496. inode = file->f_dentry->d_inode;
  497. if(inode == NULL)
  498. return 0;
  499. cifsInfo = CIFS_I(inode);
  500. if(cifsInfo->time == 0)
  501. return 1; /* directory was changed, perhaps due to unlink */
  502. else
  503. return 0;
  504. }
  505. /* find the corresponding entry in the search */
  506. /* Note that the SMB server returns search entries for . and .. which
  507. complicates logic here if we choose to parse for them and we do not
  508. assume that they are located in the findfirst return buffer.*/
  509. /* We start counting in the buffer with entry 2 and increment for every
  510. entry (do not increment for . or .. entry) */
  511. static int find_cifs_entry(const int xid, struct cifsTconInfo *pTcon,
  512. struct file *file, char **ppCurrentEntry, int *num_to_ret)
  513. {
  514. int rc = 0;
  515. int pos_in_buf = 0;
  516. loff_t first_entry_in_buffer;
  517. loff_t index_to_find = file->f_pos;
  518. struct cifsFileInfo * cifsFile = file->private_data;
  519. /* check if index in the buffer */
  520. if((cifsFile == NULL) || (ppCurrentEntry == NULL) ||
  521. (num_to_ret == NULL))
  522. return -ENOENT;
  523. *ppCurrentEntry = NULL;
  524. first_entry_in_buffer =
  525. cifsFile->srch_inf.index_of_last_entry -
  526. cifsFile->srch_inf.entries_in_buffer;
  527. /* if first entry in buf is zero then is first buffer
  528. in search response data which means it is likely . and ..
  529. will be in this buffer, although some servers do not return
  530. . and .. for the root of a drive and for those we need
  531. to start two entries earlier */
  532. #ifdef CONFIG_CIFS_DEBUG2
  533. dump_cifs_file_struct(file, "In fce ");
  534. #endif
  535. if(((index_to_find < cifsFile->srch_inf.index_of_last_entry) &&
  536. is_dir_changed(file)) ||
  537. (index_to_find < first_entry_in_buffer)) {
  538. /* close and restart search */
  539. cFYI(1,("search backing up - close and restart search"));
  540. cifsFile->invalidHandle = TRUE;
  541. CIFSFindClose(xid, pTcon, cifsFile->netfid);
  542. kfree(cifsFile->search_resume_name);
  543. cifsFile->search_resume_name = NULL;
  544. if(cifsFile->srch_inf.ntwrk_buf_start) {
  545. cFYI(1,("freeing SMB ff cache buf on search rewind"));
  546. if(cifsFile->srch_inf.smallBuf)
  547. cifs_small_buf_release(cifsFile->srch_inf.
  548. ntwrk_buf_start);
  549. else
  550. cifs_buf_release(cifsFile->srch_inf.
  551. ntwrk_buf_start);
  552. }
  553. rc = initiate_cifs_search(xid,file);
  554. if(rc) {
  555. cFYI(1,("error %d reinitiating a search on rewind",rc));
  556. return rc;
  557. }
  558. }
  559. while((index_to_find >= cifsFile->srch_inf.index_of_last_entry) &&
  560. (rc == 0) && (cifsFile->srch_inf.endOfSearch == FALSE)){
  561. cFYI(1,("calling findnext2"));
  562. rc = CIFSFindNext(xid,pTcon,cifsFile->netfid,
  563. &cifsFile->srch_inf);
  564. if(rc)
  565. return -ENOENT;
  566. }
  567. if(index_to_find < cifsFile->srch_inf.index_of_last_entry) {
  568. /* we found the buffer that contains the entry */
  569. /* scan and find it */
  570. int i;
  571. char * current_entry;
  572. char * end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
  573. smbCalcSize((struct smb_hdr *)
  574. cifsFile->srch_inf.ntwrk_buf_start);
  575. current_entry = cifsFile->srch_inf.srch_entries_start;
  576. first_entry_in_buffer = cifsFile->srch_inf.index_of_last_entry
  577. - cifsFile->srch_inf.entries_in_buffer;
  578. pos_in_buf = index_to_find - first_entry_in_buffer;
  579. cFYI(1,("found entry - pos_in_buf %d",pos_in_buf));
  580. for(i=0;(i<(pos_in_buf)) && (current_entry != NULL);i++) {
  581. /* go entry by entry figuring out which is first */
  582. current_entry = nxt_dir_entry(current_entry,end_of_smb);
  583. }
  584. if((current_entry == NULL) && (i < pos_in_buf)) {
  585. /* BB fixme - check if we should flag this error */
  586. cERROR(1,("reached end of buf searching for pos in buf"
  587. " %d index to find %lld rc %d",
  588. pos_in_buf,index_to_find,rc));
  589. }
  590. rc = 0;
  591. *ppCurrentEntry = current_entry;
  592. } else {
  593. cFYI(1,("index not in buffer - could not findnext into it"));
  594. return 0;
  595. }
  596. if(pos_in_buf >= cifsFile->srch_inf.entries_in_buffer) {
  597. cFYI(1,("can not return entries pos_in_buf beyond last entry"));
  598. *num_to_ret = 0;
  599. } else
  600. *num_to_ret = cifsFile->srch_inf.entries_in_buffer - pos_in_buf;
  601. return rc;
  602. }
  603. /* inode num, inode type and filename returned */
  604. static int cifs_get_name_from_search_buf(struct qstr *pqst,
  605. char *current_entry, __u16 level, unsigned int unicode,
  606. struct cifs_sb_info * cifs_sb, ino_t *pinum)
  607. {
  608. int rc = 0;
  609. unsigned int len = 0;
  610. char * filename;
  611. struct nls_table * nlt = cifs_sb->local_nls;
  612. *pinum = 0;
  613. if(level == SMB_FIND_FILE_UNIX) {
  614. FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
  615. filename = &pFindData->FileName[0];
  616. if(unicode) {
  617. len = cifs_unicode_bytelen(filename);
  618. } else {
  619. /* BB should we make this strnlen of PATH_MAX? */
  620. len = strnlen(filename, PATH_MAX);
  621. }
  622. /* BB fixme - hash low and high 32 bits if not 64 bit arch BB fixme */
  623. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  624. *pinum = pFindData->UniqueId;
  625. } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
  626. FILE_DIRECTORY_INFO * pFindData =
  627. (FILE_DIRECTORY_INFO *)current_entry;
  628. filename = &pFindData->FileName[0];
  629. len = le32_to_cpu(pFindData->FileNameLength);
  630. } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
  631. FILE_FULL_DIRECTORY_INFO * pFindData =
  632. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  633. filename = &pFindData->FileName[0];
  634. len = le32_to_cpu(pFindData->FileNameLength);
  635. } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
  636. SEARCH_ID_FULL_DIR_INFO * pFindData =
  637. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  638. filename = &pFindData->FileName[0];
  639. len = le32_to_cpu(pFindData->FileNameLength);
  640. *pinum = pFindData->UniqueId;
  641. } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
  642. FILE_BOTH_DIRECTORY_INFO * pFindData =
  643. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  644. filename = &pFindData->FileName[0];
  645. len = le32_to_cpu(pFindData->FileNameLength);
  646. } else {
  647. cFYI(1,("Unknown findfirst level %d",level));
  648. return -EINVAL;
  649. }
  650. if(unicode) {
  651. /* BB fixme - test with long names */
  652. /* Note converted filename can be longer than in unicode */
  653. if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR)
  654. pqst->len = cifs_convertUCSpath((char *)pqst->name,
  655. (__le16 *)filename, len/2, nlt);
  656. else
  657. pqst->len = cifs_strfromUCS_le((char *)pqst->name,
  658. (__le16 *)filename,len/2,nlt);
  659. } else {
  660. pqst->name = filename;
  661. pqst->len = len;
  662. }
  663. pqst->hash = full_name_hash(pqst->name,pqst->len);
  664. /* cFYI(1,("filldir on %s",pqst->name)); */
  665. return rc;
  666. }
  667. static int cifs_filldir(char *pfindEntry, struct file *file,
  668. filldir_t filldir, void *direntry, char *scratch_buf)
  669. {
  670. int rc = 0;
  671. struct qstr qstring;
  672. struct cifsFileInfo * pCifsF;
  673. unsigned obj_type;
  674. ino_t inum;
  675. struct cifs_sb_info * cifs_sb;
  676. struct inode *tmp_inode;
  677. struct dentry *tmp_dentry;
  678. /* get filename and len into qstring */
  679. /* get dentry */
  680. /* decide whether to create and populate ionde */
  681. if((direntry == NULL) || (file == NULL))
  682. return -EINVAL;
  683. pCifsF = file->private_data;
  684. if((scratch_buf == NULL) || (pfindEntry == NULL) || (pCifsF == NULL))
  685. return -ENOENT;
  686. if(file->f_dentry == NULL)
  687. return -ENOENT;
  688. rc = cifs_entry_is_dot(pfindEntry,pCifsF);
  689. /* skip . and .. since we added them first */
  690. if(rc != 0)
  691. return 0;
  692. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  693. qstring.name = scratch_buf;
  694. rc = cifs_get_name_from_search_buf(&qstring,pfindEntry,
  695. pCifsF->srch_inf.info_level,
  696. pCifsF->srch_inf.unicode,cifs_sb,
  697. &inum /* returned */);
  698. if(rc)
  699. return rc;
  700. rc = construct_dentry(&qstring,file,&tmp_inode, &tmp_dentry);
  701. if((tmp_inode == NULL) || (tmp_dentry == NULL))
  702. return -ENOMEM;
  703. if(rc) {
  704. /* inode created, we need to hash it with right inode number */
  705. if(inum != 0) {
  706. /* BB fixme - hash the 2 32 quantities bits together if necessary BB */
  707. tmp_inode->i_ino = inum;
  708. }
  709. insert_inode_hash(tmp_inode);
  710. }
  711. /* we pass in rc below, indicating whether it is a new inode,
  712. so we can figure out whether to invalidate the inode cached
  713. data if the file has changed */
  714. if(pCifsF->srch_inf.info_level == SMB_FIND_FILE_UNIX) {
  715. unix_fill_in_inode(tmp_inode,
  716. (FILE_UNIX_INFO *)pfindEntry,&obj_type, rc);
  717. } else {
  718. fill_in_inode(tmp_inode,
  719. (FILE_DIRECTORY_INFO *)pfindEntry,&obj_type, rc);
  720. }
  721. rc = filldir(direntry,qstring.name,qstring.len,file->f_pos,
  722. tmp_inode->i_ino,obj_type);
  723. if(rc) {
  724. cFYI(1,("filldir rc = %d",rc));
  725. }
  726. dput(tmp_dentry);
  727. return rc;
  728. }
  729. static int cifs_save_resume_key(const char *current_entry,
  730. struct cifsFileInfo *cifsFile)
  731. {
  732. int rc = 0;
  733. unsigned int len = 0;
  734. __u16 level;
  735. char * filename;
  736. if((cifsFile == NULL) || (current_entry == NULL))
  737. return -EINVAL;
  738. level = cifsFile->srch_inf.info_level;
  739. if(level == SMB_FIND_FILE_UNIX) {
  740. FILE_UNIX_INFO * pFindData = (FILE_UNIX_INFO *)current_entry;
  741. filename = &pFindData->FileName[0];
  742. if(cifsFile->srch_inf.unicode) {
  743. len = cifs_unicode_bytelen(filename);
  744. } else {
  745. /* BB should we make this strnlen of PATH_MAX? */
  746. len = strnlen(filename, PATH_MAX);
  747. }
  748. cifsFile->srch_inf.resume_key = pFindData->ResumeKey;
  749. } else if(level == SMB_FIND_FILE_DIRECTORY_INFO) {
  750. FILE_DIRECTORY_INFO * pFindData =
  751. (FILE_DIRECTORY_INFO *)current_entry;
  752. filename = &pFindData->FileName[0];
  753. len = le32_to_cpu(pFindData->FileNameLength);
  754. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  755. } else if(level == SMB_FIND_FILE_FULL_DIRECTORY_INFO) {
  756. FILE_FULL_DIRECTORY_INFO * pFindData =
  757. (FILE_FULL_DIRECTORY_INFO *)current_entry;
  758. filename = &pFindData->FileName[0];
  759. len = le32_to_cpu(pFindData->FileNameLength);
  760. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  761. } else if(level == SMB_FIND_FILE_ID_FULL_DIR_INFO) {
  762. SEARCH_ID_FULL_DIR_INFO * pFindData =
  763. (SEARCH_ID_FULL_DIR_INFO *)current_entry;
  764. filename = &pFindData->FileName[0];
  765. len = le32_to_cpu(pFindData->FileNameLength);
  766. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  767. } else if(level == SMB_FIND_FILE_BOTH_DIRECTORY_INFO) {
  768. FILE_BOTH_DIRECTORY_INFO * pFindData =
  769. (FILE_BOTH_DIRECTORY_INFO *)current_entry;
  770. filename = &pFindData->FileName[0];
  771. len = le32_to_cpu(pFindData->FileNameLength);
  772. cifsFile->srch_inf.resume_key = pFindData->FileIndex;
  773. } else {
  774. cFYI(1,("Unknown findfirst level %d",level));
  775. return -EINVAL;
  776. }
  777. cifsFile->srch_inf.resume_name_len = len;
  778. cifsFile->srch_inf.presume_name = filename;
  779. return rc;
  780. }
  781. int cifs_readdir(struct file *file, void *direntry, filldir_t filldir)
  782. {
  783. int rc = 0;
  784. int xid,i;
  785. struct cifs_sb_info *cifs_sb;
  786. struct cifsTconInfo *pTcon;
  787. struct cifsFileInfo *cifsFile = NULL;
  788. char * current_entry;
  789. int num_to_fill = 0;
  790. char * tmp_buf = NULL;
  791. char * end_of_smb;
  792. xid = GetXid();
  793. if(file->f_dentry == NULL) {
  794. FreeXid(xid);
  795. return -EIO;
  796. }
  797. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  798. pTcon = cifs_sb->tcon;
  799. if(pTcon == NULL)
  800. return -EINVAL;
  801. switch ((int) file->f_pos) {
  802. case 0:
  803. if (filldir(direntry, ".", 1, file->f_pos,
  804. file->f_dentry->d_inode->i_ino, DT_DIR) < 0) {
  805. cERROR(1, ("Filldir for current dir failed"));
  806. rc = -ENOMEM;
  807. break;
  808. }
  809. file->f_pos++;
  810. case 1:
  811. if (filldir(direntry, "..", 2, file->f_pos,
  812. file->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
  813. cERROR(1, ("Filldir for parent dir failed"));
  814. rc = -ENOMEM;
  815. break;
  816. }
  817. file->f_pos++;
  818. default:
  819. /* 1) If search is active,
  820. is in current search buffer?
  821. if it before then restart search
  822. if after then keep searching till find it */
  823. if(file->private_data == NULL) {
  824. rc = initiate_cifs_search(xid,file);
  825. cFYI(1,("initiate cifs search rc %d",rc));
  826. if(rc) {
  827. FreeXid(xid);
  828. return rc;
  829. }
  830. }
  831. if(file->private_data == NULL) {
  832. rc = -EINVAL;
  833. FreeXid(xid);
  834. return rc;
  835. }
  836. cifsFile = file->private_data;
  837. if (cifsFile->srch_inf.endOfSearch) {
  838. if(cifsFile->srch_inf.emptyDir) {
  839. cFYI(1, ("End of search, empty dir"));
  840. rc = 0;
  841. break;
  842. }
  843. } /* else {
  844. cifsFile->invalidHandle = TRUE;
  845. CIFSFindClose(xid, pTcon, cifsFile->netfid);
  846. }
  847. kfree(cifsFile->search_resume_name);
  848. cifsFile->search_resume_name = NULL; */
  849. rc = find_cifs_entry(xid,pTcon, file,
  850. &current_entry,&num_to_fill);
  851. if(rc) {
  852. cFYI(1,("fce error %d",rc));
  853. goto rddir2_exit;
  854. } else if (current_entry != NULL) {
  855. cFYI(1,("entry %lld found",file->f_pos));
  856. } else {
  857. cFYI(1,("could not find entry"));
  858. goto rddir2_exit;
  859. }
  860. cFYI(1,("loop through %d times filling dir for net buf %p",
  861. num_to_fill,cifsFile->srch_inf.ntwrk_buf_start));
  862. end_of_smb = cifsFile->srch_inf.ntwrk_buf_start +
  863. smbCalcSize((struct smb_hdr *)
  864. cifsFile->srch_inf.ntwrk_buf_start);
  865. /* To be safe - for UCS to UTF-8 with strings loaded
  866. with the rare long characters alloc more to account for
  867. such multibyte target UTF-8 characters. cifs_unicode.c,
  868. which actually does the conversion, has the same limit */
  869. tmp_buf = kmalloc((2 * NAME_MAX) + 4, GFP_KERNEL);
  870. for(i=0;(i<num_to_fill) && (rc == 0);i++) {
  871. if(current_entry == NULL) {
  872. /* evaluate whether this case is an error */
  873. cERROR(1,("past end of SMB num to fill %d i %d",
  874. num_to_fill, i));
  875. break;
  876. }
  877. /* if buggy server returns . and .. late do
  878. we want to check for that here? */
  879. rc = cifs_filldir(current_entry, file,
  880. filldir, direntry,tmp_buf);
  881. file->f_pos++;
  882. if(file->f_pos ==
  883. cifsFile->srch_inf.index_of_last_entry) {
  884. cFYI(1,("last entry in buf at pos %lld %s",
  885. file->f_pos,tmp_buf));
  886. cifs_save_resume_key(current_entry,cifsFile);
  887. break;
  888. } else
  889. current_entry = nxt_dir_entry(current_entry,
  890. end_of_smb);
  891. }
  892. kfree(tmp_buf);
  893. break;
  894. } /* end switch */
  895. rddir2_exit:
  896. FreeXid(xid);
  897. return rc;
  898. }