readdir.c 34 KB

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