readdir.c 35 KB

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