readdir.c 26 KB

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