readdir.c 33 KB

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