readdir.c 34 KB

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