readdir.c 30 KB

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