file.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829
  1. /*
  2. * fs/cifs/file.c
  3. *
  4. * vfs operations that deal with files
  5. *
  6. * Copyright (C) International Business Machines Corp., 2002,2003
  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/backing-dev.h>
  25. #include <linux/stat.h>
  26. #include <linux/fcntl.h>
  27. #include <linux/mpage.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/pagevec.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/writeback.h>
  32. #include <asm/div64.h>
  33. #include "cifsfs.h"
  34. #include "cifspdu.h"
  35. #include "cifsglob.h"
  36. #include "cifsproto.h"
  37. #include "cifs_unicode.h"
  38. #include "cifs_debug.h"
  39. #include "cifs_fs_sb.h"
  40. static inline struct cifsFileInfo *cifs_init_private(
  41. struct cifsFileInfo *private_data, struct inode *inode,
  42. struct file *file, __u16 netfid)
  43. {
  44. memset(private_data, 0, sizeof(struct cifsFileInfo));
  45. private_data->netfid = netfid;
  46. private_data->pid = current->tgid;
  47. init_MUTEX(&private_data->fh_sem);
  48. private_data->pfile = file; /* needed for writepage */
  49. private_data->pInode = inode;
  50. private_data->invalidHandle = FALSE;
  51. private_data->closePend = FALSE;
  52. return private_data;
  53. }
  54. static inline int cifs_convert_flags(unsigned int flags)
  55. {
  56. if ((flags & O_ACCMODE) == O_RDONLY)
  57. return GENERIC_READ;
  58. else if ((flags & O_ACCMODE) == O_WRONLY)
  59. return GENERIC_WRITE;
  60. else if ((flags & O_ACCMODE) == O_RDWR) {
  61. /* GENERIC_ALL is too much permission to request
  62. can cause unnecessary access denied on create */
  63. /* return GENERIC_ALL; */
  64. return (GENERIC_READ | GENERIC_WRITE);
  65. }
  66. return 0x20197;
  67. }
  68. static inline int cifs_get_disposition(unsigned int flags)
  69. {
  70. if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  71. return FILE_CREATE;
  72. else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  73. return FILE_OVERWRITE_IF;
  74. else if ((flags & O_CREAT) == O_CREAT)
  75. return FILE_OPEN_IF;
  76. else
  77. return FILE_OPEN;
  78. }
  79. /* all arguments to this function must be checked for validity in caller */
  80. static inline int cifs_open_inode_helper(struct inode *inode, struct file *file,
  81. struct cifsInodeInfo *pCifsInode, struct cifsFileInfo *pCifsFile,
  82. struct cifsTconInfo *pTcon, int *oplock, FILE_ALL_INFO *buf,
  83. char *full_path, int xid)
  84. {
  85. struct timespec temp;
  86. int rc;
  87. /* want handles we can use to read with first
  88. in the list so we do not have to walk the
  89. list to search for one in prepare_write */
  90. if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
  91. list_add_tail(&pCifsFile->flist,
  92. &pCifsInode->openFileList);
  93. } else {
  94. list_add(&pCifsFile->flist,
  95. &pCifsInode->openFileList);
  96. }
  97. write_unlock(&GlobalSMBSeslock);
  98. write_unlock(&file->f_owner.lock);
  99. if (pCifsInode->clientCanCacheRead) {
  100. /* we have the inode open somewhere else
  101. no need to discard cache data */
  102. goto client_can_cache;
  103. }
  104. /* BB need same check in cifs_create too? */
  105. /* if not oplocked, invalidate inode pages if mtime or file
  106. size changed */
  107. temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime));
  108. if (timespec_equal(&file->f_dentry->d_inode->i_mtime, &temp) &&
  109. (file->f_dentry->d_inode->i_size ==
  110. (loff_t)le64_to_cpu(buf->EndOfFile))) {
  111. cFYI(1, ("inode unchanged on server"));
  112. } else {
  113. if (file->f_dentry->d_inode->i_mapping) {
  114. /* BB no need to lock inode until after invalidate
  115. since namei code should already have it locked? */
  116. filemap_fdatawrite(file->f_dentry->d_inode->i_mapping);
  117. filemap_fdatawait(file->f_dentry->d_inode->i_mapping);
  118. }
  119. cFYI(1, ("invalidating remote inode since open detected it "
  120. "changed"));
  121. invalidate_remote_inode(file->f_dentry->d_inode);
  122. }
  123. client_can_cache:
  124. if (pTcon->ses->capabilities & CAP_UNIX)
  125. rc = cifs_get_inode_info_unix(&file->f_dentry->d_inode,
  126. full_path, inode->i_sb, xid);
  127. else
  128. rc = cifs_get_inode_info(&file->f_dentry->d_inode,
  129. full_path, buf, inode->i_sb, xid);
  130. if ((*oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  131. pCifsInode->clientCanCacheAll = TRUE;
  132. pCifsInode->clientCanCacheRead = TRUE;
  133. cFYI(1, ("Exclusive Oplock granted on inode %p",
  134. file->f_dentry->d_inode));
  135. } else if ((*oplock & 0xF) == OPLOCK_READ)
  136. pCifsInode->clientCanCacheRead = TRUE;
  137. return rc;
  138. }
  139. int cifs_open(struct inode *inode, struct file *file)
  140. {
  141. int rc = -EACCES;
  142. int xid, oplock;
  143. struct cifs_sb_info *cifs_sb;
  144. struct cifsTconInfo *pTcon;
  145. struct cifsFileInfo *pCifsFile;
  146. struct cifsInodeInfo *pCifsInode;
  147. struct list_head *tmp;
  148. char *full_path = NULL;
  149. int desiredAccess;
  150. int disposition;
  151. __u16 netfid;
  152. FILE_ALL_INFO *buf = NULL;
  153. xid = GetXid();
  154. cifs_sb = CIFS_SB(inode->i_sb);
  155. pTcon = cifs_sb->tcon;
  156. if (file->f_flags & O_CREAT) {
  157. /* search inode for this file and fill in file->private_data */
  158. pCifsInode = CIFS_I(file->f_dentry->d_inode);
  159. read_lock(&GlobalSMBSeslock);
  160. list_for_each(tmp, &pCifsInode->openFileList) {
  161. pCifsFile = list_entry(tmp, struct cifsFileInfo,
  162. flist);
  163. if ((pCifsFile->pfile == NULL) &&
  164. (pCifsFile->pid == current->tgid)) {
  165. /* mode set in cifs_create */
  166. /* needed for writepage */
  167. pCifsFile->pfile = file;
  168. file->private_data = pCifsFile;
  169. break;
  170. }
  171. }
  172. read_unlock(&GlobalSMBSeslock);
  173. if (file->private_data != NULL) {
  174. rc = 0;
  175. FreeXid(xid);
  176. return rc;
  177. } else {
  178. if (file->f_flags & O_EXCL)
  179. cERROR(1, ("could not find file instance for "
  180. "new file %p ", file));
  181. }
  182. }
  183. down(&inode->i_sb->s_vfs_rename_sem);
  184. full_path = build_path_from_dentry(file->f_dentry);
  185. up(&inode->i_sb->s_vfs_rename_sem);
  186. if (full_path == NULL) {
  187. FreeXid(xid);
  188. return -ENOMEM;
  189. }
  190. cFYI(1, (" inode = 0x%p file flags are 0x%x for %s",
  191. inode, file->f_flags, full_path));
  192. desiredAccess = cifs_convert_flags(file->f_flags);
  193. /*********************************************************************
  194. * open flag mapping table:
  195. *
  196. * POSIX Flag CIFS Disposition
  197. * ---------- ----------------
  198. * O_CREAT FILE_OPEN_IF
  199. * O_CREAT | O_EXCL FILE_CREATE
  200. * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
  201. * O_TRUNC FILE_OVERWRITE
  202. * none of the above FILE_OPEN
  203. *
  204. * Note that there is not a direct match between disposition
  205. * FILE_SUPERSEDE (ie create whether or not file exists although
  206. * O_CREAT | O_TRUNC is similar but truncates the existing
  207. * file rather than creating a new file as FILE_SUPERSEDE does
  208. * (which uses the attributes / metadata passed in on open call)
  209. *?
  210. *? O_SYNC is a reasonable match to CIFS writethrough flag
  211. *? and the read write flags match reasonably. O_LARGEFILE
  212. *? is irrelevant because largefile support is always used
  213. *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
  214. * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
  215. *********************************************************************/
  216. disposition = cifs_get_disposition(file->f_flags);
  217. if (oplockEnabled)
  218. oplock = REQ_OPLOCK;
  219. else
  220. oplock = FALSE;
  221. /* BB pass O_SYNC flag through on file attributes .. BB */
  222. /* Also refresh inode by passing in file_info buf returned by SMBOpen
  223. and calling get_inode_info with returned buf (at least helps
  224. non-Unix server case) */
  225. /* BB we can not do this if this is the second open of a file
  226. and the first handle has writebehind data, we might be
  227. able to simply do a filemap_fdatawrite/filemap_fdatawait first */
  228. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  229. if (!buf) {
  230. rc = -ENOMEM;
  231. goto out;
  232. }
  233. rc = CIFSSMBOpen(xid, pTcon, full_path, disposition, desiredAccess,
  234. CREATE_NOT_DIR, &netfid, &oplock, buf,
  235. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
  236. & CIFS_MOUNT_MAP_SPECIAL_CHR);
  237. if (rc == -EIO) {
  238. /* Old server, try legacy style OpenX */
  239. rc = SMBLegacyOpen(xid, pTcon, full_path, disposition,
  240. desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf,
  241. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
  242. & CIFS_MOUNT_MAP_SPECIAL_CHR);
  243. }
  244. if (rc) {
  245. cFYI(1, ("cifs_open returned 0x%x ", rc));
  246. goto out;
  247. }
  248. file->private_data =
  249. kmalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  250. if (file->private_data == NULL) {
  251. rc = -ENOMEM;
  252. goto out;
  253. }
  254. pCifsFile = cifs_init_private(file->private_data, inode, file, netfid);
  255. write_lock(&file->f_owner.lock);
  256. write_lock(&GlobalSMBSeslock);
  257. list_add(&pCifsFile->tlist, &pTcon->openFileList);
  258. pCifsInode = CIFS_I(file->f_dentry->d_inode);
  259. if (pCifsInode) {
  260. rc = cifs_open_inode_helper(inode, file, pCifsInode,
  261. pCifsFile, pTcon,
  262. &oplock, buf, full_path, xid);
  263. } else {
  264. write_unlock(&GlobalSMBSeslock);
  265. write_unlock(&file->f_owner.lock);
  266. }
  267. if (oplock & CIFS_CREATE_ACTION) {
  268. /* time to set mode which we can not set earlier due to
  269. problems creating new read-only files */
  270. if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
  271. CIFSSMBUnixSetPerms(xid, pTcon, full_path,
  272. inode->i_mode,
  273. (__u64)-1, (__u64)-1, 0 /* dev */,
  274. cifs_sb->local_nls,
  275. cifs_sb->mnt_cifs_flags &
  276. CIFS_MOUNT_MAP_SPECIAL_CHR);
  277. } else {
  278. /* BB implement via Windows security descriptors eg
  279. CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
  280. -1, -1, local_nls);
  281. in the meantime could set r/o dos attribute when
  282. perms are eg: mode & 0222 == 0 */
  283. }
  284. }
  285. out:
  286. kfree(buf);
  287. kfree(full_path);
  288. FreeXid(xid);
  289. return rc;
  290. }
  291. /* Try to reaquire byte range locks that were released when session */
  292. /* to server was lost */
  293. static int cifs_relock_file(struct cifsFileInfo *cifsFile)
  294. {
  295. int rc = 0;
  296. /* BB list all locks open on this file and relock */
  297. return rc;
  298. }
  299. static int cifs_reopen_file(struct inode *inode, struct file *file,
  300. int can_flush)
  301. {
  302. int rc = -EACCES;
  303. int xid, oplock;
  304. struct cifs_sb_info *cifs_sb;
  305. struct cifsTconInfo *pTcon;
  306. struct cifsFileInfo *pCifsFile;
  307. struct cifsInodeInfo *pCifsInode;
  308. char *full_path = NULL;
  309. int desiredAccess;
  310. int disposition = FILE_OPEN;
  311. __u16 netfid;
  312. if (inode == NULL)
  313. return -EBADF;
  314. if (file->private_data) {
  315. pCifsFile = (struct cifsFileInfo *)file->private_data;
  316. } else
  317. return -EBADF;
  318. xid = GetXid();
  319. down(&pCifsFile->fh_sem);
  320. if (pCifsFile->invalidHandle == FALSE) {
  321. up(&pCifsFile->fh_sem);
  322. FreeXid(xid);
  323. return 0;
  324. }
  325. if (file->f_dentry == NULL) {
  326. up(&pCifsFile->fh_sem);
  327. cFYI(1, ("failed file reopen, no valid name if dentry freed"));
  328. FreeXid(xid);
  329. return -EBADF;
  330. }
  331. cifs_sb = CIFS_SB(inode->i_sb);
  332. pTcon = cifs_sb->tcon;
  333. /* can not grab rename sem here because various ops, including
  334. those that already have the rename sem can end up causing writepage
  335. to get called and if the server was down that means we end up here,
  336. and we can never tell if the caller already has the rename_sem */
  337. full_path = build_path_from_dentry(file->f_dentry);
  338. if (full_path == NULL) {
  339. up(&pCifsFile->fh_sem);
  340. FreeXid(xid);
  341. return -ENOMEM;
  342. }
  343. cFYI(1, (" inode = 0x%p file flags are 0x%x for %s",
  344. inode, file->f_flags,full_path));
  345. desiredAccess = cifs_convert_flags(file->f_flags);
  346. if (oplockEnabled)
  347. oplock = REQ_OPLOCK;
  348. else
  349. oplock = FALSE;
  350. /* Can not refresh inode by passing in file_info buf to be returned
  351. by SMBOpen and then calling get_inode_info with returned buf
  352. since file might have write behind data that needs to be flushed
  353. and server version of file size can be stale. If we knew for sure
  354. that inode was not dirty locally we could do this */
  355. /* buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  356. if (buf == 0) {
  357. up(&pCifsFile->fh_sem);
  358. kfree(full_path);
  359. FreeXid(xid);
  360. return -ENOMEM;
  361. } */
  362. rc = CIFSSMBOpen(xid, pTcon, full_path, disposition, desiredAccess,
  363. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  364. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  365. CIFS_MOUNT_MAP_SPECIAL_CHR);
  366. if (rc) {
  367. up(&pCifsFile->fh_sem);
  368. cFYI(1, ("cifs_open returned 0x%x ", rc));
  369. cFYI(1, ("oplock: %d ", oplock));
  370. } else {
  371. pCifsFile->netfid = netfid;
  372. pCifsFile->invalidHandle = FALSE;
  373. up(&pCifsFile->fh_sem);
  374. pCifsInode = CIFS_I(inode);
  375. if (pCifsInode) {
  376. if (can_flush) {
  377. filemap_fdatawrite(inode->i_mapping);
  378. filemap_fdatawait(inode->i_mapping);
  379. /* temporarily disable caching while we
  380. go to server to get inode info */
  381. pCifsInode->clientCanCacheAll = FALSE;
  382. pCifsInode->clientCanCacheRead = FALSE;
  383. if (pTcon->ses->capabilities & CAP_UNIX)
  384. rc = cifs_get_inode_info_unix(&inode,
  385. full_path, inode->i_sb, xid);
  386. else
  387. rc = cifs_get_inode_info(&inode,
  388. full_path, NULL, inode->i_sb,
  389. xid);
  390. } /* else we are writing out data to server already
  391. and could deadlock if we tried to flush data, and
  392. since we do not know if we have data that would
  393. invalidate the current end of file on the server
  394. we can not go to the server to get the new inod
  395. info */
  396. if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  397. pCifsInode->clientCanCacheAll = TRUE;
  398. pCifsInode->clientCanCacheRead = TRUE;
  399. cFYI(1, ("Exclusive Oplock granted on inode %p",
  400. file->f_dentry->d_inode));
  401. } else if ((oplock & 0xF) == OPLOCK_READ) {
  402. pCifsInode->clientCanCacheRead = TRUE;
  403. pCifsInode->clientCanCacheAll = FALSE;
  404. } else {
  405. pCifsInode->clientCanCacheRead = FALSE;
  406. pCifsInode->clientCanCacheAll = FALSE;
  407. }
  408. cifs_relock_file(pCifsFile);
  409. }
  410. }
  411. kfree(full_path);
  412. FreeXid(xid);
  413. return rc;
  414. }
  415. int cifs_close(struct inode *inode, struct file *file)
  416. {
  417. int rc = 0;
  418. int xid;
  419. struct cifs_sb_info *cifs_sb;
  420. struct cifsTconInfo *pTcon;
  421. struct cifsFileInfo *pSMBFile =
  422. (struct cifsFileInfo *)file->private_data;
  423. xid = GetXid();
  424. cifs_sb = CIFS_SB(inode->i_sb);
  425. pTcon = cifs_sb->tcon;
  426. if (pSMBFile) {
  427. pSMBFile->closePend = TRUE;
  428. write_lock(&file->f_owner.lock);
  429. if (pTcon) {
  430. /* no sense reconnecting to close a file that is
  431. already closed */
  432. if (pTcon->tidStatus != CifsNeedReconnect) {
  433. write_unlock(&file->f_owner.lock);
  434. rc = CIFSSMBClose(xid, pTcon,
  435. pSMBFile->netfid);
  436. write_lock(&file->f_owner.lock);
  437. }
  438. }
  439. write_lock(&GlobalSMBSeslock);
  440. list_del(&pSMBFile->flist);
  441. list_del(&pSMBFile->tlist);
  442. write_unlock(&GlobalSMBSeslock);
  443. write_unlock(&file->f_owner.lock);
  444. kfree(pSMBFile->search_resume_name);
  445. kfree(file->private_data);
  446. file->private_data = NULL;
  447. } else
  448. rc = -EBADF;
  449. if (list_empty(&(CIFS_I(inode)->openFileList))) {
  450. cFYI(1, ("closing last open instance for inode %p", inode));
  451. /* if the file is not open we do not know if we can cache info
  452. on this inode, much less write behind and read ahead */
  453. CIFS_I(inode)->clientCanCacheRead = FALSE;
  454. CIFS_I(inode)->clientCanCacheAll = FALSE;
  455. }
  456. if ((rc ==0) && CIFS_I(inode)->write_behind_rc)
  457. rc = CIFS_I(inode)->write_behind_rc;
  458. FreeXid(xid);
  459. return rc;
  460. }
  461. int cifs_closedir(struct inode *inode, struct file *file)
  462. {
  463. int rc = 0;
  464. int xid;
  465. struct cifsFileInfo *pCFileStruct =
  466. (struct cifsFileInfo *)file->private_data;
  467. char *ptmp;
  468. cFYI(1, ("Closedir inode = 0x%p with ", inode));
  469. xid = GetXid();
  470. if (pCFileStruct) {
  471. struct cifsTconInfo *pTcon;
  472. struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  473. pTcon = cifs_sb->tcon;
  474. cFYI(1, ("Freeing private data in close dir"));
  475. if ((pCFileStruct->srch_inf.endOfSearch == FALSE) &&
  476. (pCFileStruct->invalidHandle == FALSE)) {
  477. pCFileStruct->invalidHandle = TRUE;
  478. rc = CIFSFindClose(xid, pTcon, pCFileStruct->netfid);
  479. cFYI(1, ("Closing uncompleted readdir with rc %d",
  480. rc));
  481. /* not much we can do if it fails anyway, ignore rc */
  482. rc = 0;
  483. }
  484. ptmp = pCFileStruct->srch_inf.ntwrk_buf_start;
  485. if (ptmp) {
  486. /* BB removeme BB */ cFYI(1, ("freeing smb buf in srch struct in closedir"));
  487. pCFileStruct->srch_inf.ntwrk_buf_start = NULL;
  488. cifs_buf_release(ptmp);
  489. }
  490. ptmp = pCFileStruct->search_resume_name;
  491. if (ptmp) {
  492. /* BB removeme BB */ cFYI(1, ("freeing resume name in closedir"));
  493. pCFileStruct->search_resume_name = NULL;
  494. kfree(ptmp);
  495. }
  496. kfree(file->private_data);
  497. file->private_data = NULL;
  498. }
  499. /* BB can we lock the filestruct while this is going on? */
  500. FreeXid(xid);
  501. return rc;
  502. }
  503. int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock)
  504. {
  505. int rc, xid;
  506. __u32 lockType = LOCKING_ANDX_LARGE_FILES;
  507. __u32 numLock = 0;
  508. __u32 numUnlock = 0;
  509. __u64 length;
  510. int wait_flag = FALSE;
  511. struct cifs_sb_info *cifs_sb;
  512. struct cifsTconInfo *pTcon;
  513. length = 1 + pfLock->fl_end - pfLock->fl_start;
  514. rc = -EACCES;
  515. xid = GetXid();
  516. cFYI(1, ("Lock parm: 0x%x flockflags: "
  517. "0x%x flocktype: 0x%x start: %lld end: %lld",
  518. cmd, pfLock->fl_flags, pfLock->fl_type, pfLock->fl_start,
  519. pfLock->fl_end));
  520. if (pfLock->fl_flags & FL_POSIX)
  521. cFYI(1, ("Posix "));
  522. if (pfLock->fl_flags & FL_FLOCK)
  523. cFYI(1, ("Flock "));
  524. if (pfLock->fl_flags & FL_SLEEP) {
  525. cFYI(1, ("Blocking lock "));
  526. wait_flag = TRUE;
  527. }
  528. if (pfLock->fl_flags & FL_ACCESS)
  529. cFYI(1, ("Process suspended by mandatory locking - "
  530. "not implemented yet "));
  531. if (pfLock->fl_flags & FL_LEASE)
  532. cFYI(1, ("Lease on file - not implemented yet"));
  533. if (pfLock->fl_flags &
  534. (~(FL_POSIX | FL_FLOCK | FL_SLEEP | FL_ACCESS | FL_LEASE)))
  535. cFYI(1, ("Unknown lock flags 0x%x", pfLock->fl_flags));
  536. if (pfLock->fl_type == F_WRLCK) {
  537. cFYI(1, ("F_WRLCK "));
  538. numLock = 1;
  539. } else if (pfLock->fl_type == F_UNLCK) {
  540. cFYI(1, ("F_UNLCK "));
  541. numUnlock = 1;
  542. } else if (pfLock->fl_type == F_RDLCK) {
  543. cFYI(1, ("F_RDLCK "));
  544. lockType |= LOCKING_ANDX_SHARED_LOCK;
  545. numLock = 1;
  546. } else if (pfLock->fl_type == F_EXLCK) {
  547. cFYI(1, ("F_EXLCK "));
  548. numLock = 1;
  549. } else if (pfLock->fl_type == F_SHLCK) {
  550. cFYI(1, ("F_SHLCK "));
  551. lockType |= LOCKING_ANDX_SHARED_LOCK;
  552. numLock = 1;
  553. } else
  554. cFYI(1, ("Unknown type of lock "));
  555. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  556. pTcon = cifs_sb->tcon;
  557. if (file->private_data == NULL) {
  558. FreeXid(xid);
  559. return -EBADF;
  560. }
  561. if (IS_GETLK(cmd)) {
  562. rc = CIFSSMBLock(xid, pTcon,
  563. ((struct cifsFileInfo *)file->
  564. private_data)->netfid,
  565. length,
  566. pfLock->fl_start, 0, 1, lockType,
  567. 0 /* wait flag */ );
  568. if (rc == 0) {
  569. rc = CIFSSMBLock(xid, pTcon,
  570. ((struct cifsFileInfo *) file->
  571. private_data)->netfid,
  572. length,
  573. pfLock->fl_start, 1 /* numUnlock */ ,
  574. 0 /* numLock */ , lockType,
  575. 0 /* wait flag */ );
  576. pfLock->fl_type = F_UNLCK;
  577. if (rc != 0)
  578. cERROR(1, ("Error unlocking previously locked "
  579. "range %d during test of lock ",
  580. rc));
  581. rc = 0;
  582. } else {
  583. /* if rc == ERR_SHARING_VIOLATION ? */
  584. rc = 0; /* do not change lock type to unlock
  585. since range in use */
  586. }
  587. FreeXid(xid);
  588. return rc;
  589. }
  590. rc = CIFSSMBLock(xid, pTcon,
  591. ((struct cifsFileInfo *) file->private_data)->
  592. netfid, length,
  593. pfLock->fl_start, numUnlock, numLock, lockType,
  594. wait_flag);
  595. if (pfLock->fl_flags & FL_POSIX)
  596. posix_lock_file_wait(file, pfLock);
  597. FreeXid(xid);
  598. return rc;
  599. }
  600. ssize_t cifs_user_write(struct file *file, const char __user *write_data,
  601. size_t write_size, loff_t *poffset)
  602. {
  603. int rc = 0;
  604. unsigned int bytes_written = 0;
  605. unsigned int total_written;
  606. struct cifs_sb_info *cifs_sb;
  607. struct cifsTconInfo *pTcon;
  608. int xid, long_op;
  609. struct cifsFileInfo *open_file;
  610. if (file->f_dentry == NULL)
  611. return -EBADF;
  612. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  613. if (cifs_sb == NULL)
  614. return -EBADF;
  615. pTcon = cifs_sb->tcon;
  616. /* cFYI(1,
  617. (" write %d bytes to offset %lld of %s", write_size,
  618. *poffset, file->f_dentry->d_name.name)); */
  619. if (file->private_data == NULL)
  620. return -EBADF;
  621. else
  622. open_file = (struct cifsFileInfo *) file->private_data;
  623. xid = GetXid();
  624. if (file->f_dentry->d_inode == NULL) {
  625. FreeXid(xid);
  626. return -EBADF;
  627. }
  628. if (*poffset > file->f_dentry->d_inode->i_size)
  629. long_op = 2; /* writes past end of file can take a long time */
  630. else
  631. long_op = 1;
  632. for (total_written = 0; write_size > total_written;
  633. total_written += bytes_written) {
  634. rc = -EAGAIN;
  635. while (rc == -EAGAIN) {
  636. if (file->private_data == NULL) {
  637. /* file has been closed on us */
  638. FreeXid(xid);
  639. /* if we have gotten here we have written some data
  640. and blocked, and the file has been freed on us while
  641. we blocked so return what we managed to write */
  642. return total_written;
  643. }
  644. if (open_file->closePend) {
  645. FreeXid(xid);
  646. if (total_written)
  647. return total_written;
  648. else
  649. return -EBADF;
  650. }
  651. if (open_file->invalidHandle) {
  652. if ((file->f_dentry == NULL) ||
  653. (file->f_dentry->d_inode == NULL)) {
  654. FreeXid(xid);
  655. return total_written;
  656. }
  657. /* we could deadlock if we called
  658. filemap_fdatawait from here so tell
  659. reopen_file not to flush data to server
  660. now */
  661. rc = cifs_reopen_file(file->f_dentry->d_inode,
  662. file, FALSE);
  663. if (rc != 0)
  664. break;
  665. }
  666. rc = CIFSSMBWrite(xid, pTcon,
  667. open_file->netfid,
  668. min_t(const int, cifs_sb->wsize,
  669. write_size - total_written),
  670. *poffset, &bytes_written,
  671. NULL, write_data + total_written, long_op);
  672. }
  673. if (rc || (bytes_written == 0)) {
  674. if (total_written)
  675. break;
  676. else {
  677. FreeXid(xid);
  678. return rc;
  679. }
  680. } else
  681. *poffset += bytes_written;
  682. long_op = FALSE; /* subsequent writes fast -
  683. 15 seconds is plenty */
  684. }
  685. cifs_stats_bytes_written(pTcon, total_written);
  686. /* since the write may have blocked check these pointers again */
  687. if (file->f_dentry) {
  688. if (file->f_dentry->d_inode) {
  689. struct inode *inode = file->f_dentry->d_inode;
  690. inode->i_ctime = inode->i_mtime =
  691. current_fs_time(inode->i_sb);
  692. if (total_written > 0) {
  693. if (*poffset > file->f_dentry->d_inode->i_size)
  694. i_size_write(file->f_dentry->d_inode,
  695. *poffset);
  696. }
  697. mark_inode_dirty_sync(file->f_dentry->d_inode);
  698. }
  699. }
  700. FreeXid(xid);
  701. return total_written;
  702. }
  703. static ssize_t cifs_write(struct file *file, const char *write_data,
  704. size_t write_size, loff_t *poffset)
  705. {
  706. int rc = 0;
  707. unsigned int bytes_written = 0;
  708. unsigned int total_written;
  709. struct cifs_sb_info *cifs_sb;
  710. struct cifsTconInfo *pTcon;
  711. int xid, long_op;
  712. struct cifsFileInfo *open_file;
  713. if (file->f_dentry == NULL)
  714. return -EBADF;
  715. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  716. if (cifs_sb == NULL)
  717. return -EBADF;
  718. pTcon = cifs_sb->tcon;
  719. cFYI(1,("write %zd bytes to offset %lld of %s", write_size,
  720. *poffset, file->f_dentry->d_name.name));
  721. if (file->private_data == NULL)
  722. return -EBADF;
  723. else
  724. open_file = (struct cifsFileInfo *)file->private_data;
  725. xid = GetXid();
  726. if (file->f_dentry->d_inode == NULL) {
  727. FreeXid(xid);
  728. return -EBADF;
  729. }
  730. if (*poffset > file->f_dentry->d_inode->i_size)
  731. long_op = 2; /* writes past end of file can take a long time */
  732. else
  733. long_op = 1;
  734. for (total_written = 0; write_size > total_written;
  735. total_written += bytes_written) {
  736. rc = -EAGAIN;
  737. while (rc == -EAGAIN) {
  738. if (file->private_data == NULL) {
  739. /* file has been closed on us */
  740. FreeXid(xid);
  741. /* if we have gotten here we have written some data
  742. and blocked, and the file has been freed on us
  743. while we blocked so return what we managed to
  744. write */
  745. return total_written;
  746. }
  747. if (open_file->closePend) {
  748. FreeXid(xid);
  749. if (total_written)
  750. return total_written;
  751. else
  752. return -EBADF;
  753. }
  754. if (open_file->invalidHandle) {
  755. if ((file->f_dentry == NULL) ||
  756. (file->f_dentry->d_inode == NULL)) {
  757. FreeXid(xid);
  758. return total_written;
  759. }
  760. /* we could deadlock if we called
  761. filemap_fdatawait from here so tell
  762. reopen_file not to flush data to
  763. server now */
  764. rc = cifs_reopen_file(file->f_dentry->d_inode,
  765. file, FALSE);
  766. if (rc != 0)
  767. break;
  768. }
  769. #ifdef CONFIG_CIFS_EXPERIMENTAL
  770. /* BB FIXME We can not sign across two buffers yet */
  771. if((experimEnabled) && ((pTcon->ses->server->secMode &
  772. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) == 0)) {
  773. struct kvec iov[2];
  774. unsigned int len;
  775. len = min(cifs_sb->wsize,
  776. write_size - total_written);
  777. /* iov[0] is reserved for smb header */
  778. iov[1].iov_base = (char *)write_data +
  779. total_written;
  780. iov[1].iov_len = len;
  781. rc = CIFSSMBWrite2(xid, pTcon,
  782. open_file->netfid, len,
  783. *poffset, &bytes_written,
  784. iov, 1, long_op);
  785. } else
  786. /* BB FIXME fixup indentation of line below */
  787. #endif
  788. rc = CIFSSMBWrite(xid, pTcon,
  789. open_file->netfid,
  790. min_t(const int, cifs_sb->wsize,
  791. write_size - total_written),
  792. *poffset, &bytes_written,
  793. write_data + total_written, NULL, long_op);
  794. }
  795. if (rc || (bytes_written == 0)) {
  796. if (total_written)
  797. break;
  798. else {
  799. FreeXid(xid);
  800. return rc;
  801. }
  802. } else
  803. *poffset += bytes_written;
  804. long_op = FALSE; /* subsequent writes fast -
  805. 15 seconds is plenty */
  806. }
  807. cifs_stats_bytes_written(pTcon, total_written);
  808. /* since the write may have blocked check these pointers again */
  809. if (file->f_dentry) {
  810. if (file->f_dentry->d_inode) {
  811. file->f_dentry->d_inode->i_ctime =
  812. file->f_dentry->d_inode->i_mtime = CURRENT_TIME;
  813. if (total_written > 0) {
  814. if (*poffset > file->f_dentry->d_inode->i_size)
  815. i_size_write(file->f_dentry->d_inode,
  816. *poffset);
  817. }
  818. mark_inode_dirty_sync(file->f_dentry->d_inode);
  819. }
  820. }
  821. FreeXid(xid);
  822. return total_written;
  823. }
  824. struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode)
  825. {
  826. struct cifsFileInfo *open_file;
  827. int rc;
  828. read_lock(&GlobalSMBSeslock);
  829. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  830. if (open_file->closePend)
  831. continue;
  832. if (open_file->pfile &&
  833. ((open_file->pfile->f_flags & O_RDWR) ||
  834. (open_file->pfile->f_flags & O_WRONLY))) {
  835. read_unlock(&GlobalSMBSeslock);
  836. if(open_file->invalidHandle) {
  837. rc = cifs_reopen_file(&cifs_inode->vfs_inode,
  838. open_file->pfile, FALSE);
  839. /* if it fails, try another handle - might be */
  840. /* dangerous to hold up writepages with retry */
  841. if(rc) {
  842. cFYI(1,("failed on reopen file in wp"));
  843. read_lock(&GlobalSMBSeslock);
  844. continue;
  845. }
  846. }
  847. return open_file;
  848. }
  849. }
  850. read_unlock(&GlobalSMBSeslock);
  851. return NULL;
  852. }
  853. static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
  854. {
  855. struct address_space *mapping = page->mapping;
  856. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  857. char *write_data;
  858. int rc = -EFAULT;
  859. int bytes_written = 0;
  860. struct cifs_sb_info *cifs_sb;
  861. struct cifsTconInfo *pTcon;
  862. struct inode *inode;
  863. struct cifsFileInfo *open_file;
  864. if (!mapping || !mapping->host)
  865. return -EFAULT;
  866. inode = page->mapping->host;
  867. cifs_sb = CIFS_SB(inode->i_sb);
  868. pTcon = cifs_sb->tcon;
  869. offset += (loff_t)from;
  870. write_data = kmap(page);
  871. write_data += from;
  872. if ((to > PAGE_CACHE_SIZE) || (from > to)) {
  873. kunmap(page);
  874. return -EIO;
  875. }
  876. /* racing with truncate? */
  877. if (offset > mapping->host->i_size) {
  878. kunmap(page);
  879. return 0; /* don't care */
  880. }
  881. /* check to make sure that we are not extending the file */
  882. if (mapping->host->i_size - offset < (loff_t)to)
  883. to = (unsigned)(mapping->host->i_size - offset);
  884. open_file = find_writable_file(CIFS_I(mapping->host));
  885. if (open_file) {
  886. bytes_written = cifs_write(open_file->pfile, write_data,
  887. to-from, &offset);
  888. /* Does mm or vfs already set times? */
  889. inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb);
  890. if ((bytes_written > 0) && (offset)) {
  891. rc = 0;
  892. } else if (bytes_written < 0) {
  893. if (rc != -EBADF)
  894. rc = bytes_written;
  895. }
  896. } else {
  897. cFYI(1, ("No writeable filehandles for inode"));
  898. rc = -EIO;
  899. }
  900. kunmap(page);
  901. return rc;
  902. }
  903. #ifdef CONFIG_CIFS_EXPERIMENTAL
  904. static int cifs_writepages(struct address_space *mapping,
  905. struct writeback_control *wbc)
  906. {
  907. struct backing_dev_info *bdi = mapping->backing_dev_info;
  908. unsigned int bytes_to_write;
  909. unsigned int bytes_written;
  910. struct cifs_sb_info *cifs_sb;
  911. int done = 0;
  912. pgoff_t end = -1;
  913. pgoff_t index;
  914. int is_range = 0;
  915. struct kvec iov[32];
  916. int n_iov = 0;
  917. pgoff_t next;
  918. int nr_pages;
  919. __u64 offset = 0;
  920. struct cifsFileInfo *open_file = NULL;
  921. struct page *page;
  922. struct pagevec pvec;
  923. int rc = 0;
  924. int scanned = 0;
  925. int xid;
  926. cifs_sb = CIFS_SB(mapping->host->i_sb);
  927. /*
  928. * If wsize is smaller that the page cache size, default to writing
  929. * one page at a time via cifs_writepage
  930. */
  931. if (cifs_sb->wsize < PAGE_CACHE_SIZE)
  932. return generic_writepages(mapping, wbc);
  933. /* BB FIXME we do not have code to sign across multiple buffers yet,
  934. so go to older writepage style write which we can sign if needed */
  935. if((cifs_sb->tcon->ses) && (cifs_sb->tcon->ses->server))
  936. if(cifs_sb->tcon->ses->server->secMode &
  937. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  938. return generic_writepages(mapping, wbc);
  939. /*
  940. * BB: Is this meaningful for a non-block-device file system?
  941. * If it is, we should test it again after we do I/O
  942. */
  943. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  944. wbc->encountered_congestion = 1;
  945. return 0;
  946. }
  947. xid = GetXid();
  948. pagevec_init(&pvec, 0);
  949. if (wbc->sync_mode == WB_SYNC_NONE)
  950. index = mapping->writeback_index; /* Start from prev offset */
  951. else {
  952. index = 0;
  953. scanned = 1;
  954. }
  955. if (wbc->start || wbc->end) {
  956. index = wbc->start >> PAGE_CACHE_SHIFT;
  957. end = wbc->end >> PAGE_CACHE_SHIFT;
  958. is_range = 1;
  959. scanned = 1;
  960. }
  961. retry:
  962. while (!done && (index <= end) &&
  963. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  964. PAGECACHE_TAG_DIRTY,
  965. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1))) {
  966. int first;
  967. unsigned int i;
  968. if (!open_file) {
  969. open_file = find_writable_file(CIFS_I(mapping->host));
  970. if (!open_file) {
  971. pagevec_release(&pvec);
  972. cERROR(1, ("No writable handles for inode"));
  973. return -EIO;
  974. }
  975. }
  976. first = -1;
  977. next = 0;
  978. n_iov = 0;
  979. bytes_to_write = 0;
  980. for (i = 0; i < nr_pages; i++) {
  981. page = pvec.pages[i];
  982. /*
  983. * At this point we hold neither mapping->tree_lock nor
  984. * lock on the page itself: the page may be truncated or
  985. * invalidated (changing page->mapping to NULL), or even
  986. * swizzled back from swapper_space to tmpfs file
  987. * mapping
  988. */
  989. if (first < 0)
  990. lock_page(page);
  991. else if (TestSetPageLocked(page))
  992. break;
  993. if (unlikely(page->mapping != mapping)) {
  994. unlock_page(page);
  995. break;
  996. }
  997. if (unlikely(is_range) && (page->index > end)) {
  998. done = 1;
  999. unlock_page(page);
  1000. break;
  1001. }
  1002. if (next && (page->index != next)) {
  1003. /* Not next consecutive page */
  1004. unlock_page(page);
  1005. break;
  1006. }
  1007. if (wbc->sync_mode != WB_SYNC_NONE)
  1008. wait_on_page_writeback(page);
  1009. if (PageWriteback(page) ||
  1010. !test_clear_page_dirty(page)) {
  1011. unlock_page(page);
  1012. break;
  1013. }
  1014. /*
  1015. * BB can we get rid of this? pages are held by pvec
  1016. */
  1017. page_cache_get(page);
  1018. /* reserve iov[0] for the smb header */
  1019. n_iov++;
  1020. iov[n_iov].iov_base = kmap(page);
  1021. iov[n_iov].iov_len = PAGE_CACHE_SIZE;
  1022. bytes_to_write += PAGE_CACHE_SIZE;
  1023. if (first < 0) {
  1024. first = i;
  1025. offset = page_offset(page);
  1026. }
  1027. next = page->index + 1;
  1028. if (bytes_to_write + PAGE_CACHE_SIZE > cifs_sb->wsize)
  1029. break;
  1030. }
  1031. if (n_iov) {
  1032. rc = CIFSSMBWrite2(xid, cifs_sb->tcon,
  1033. open_file->netfid, bytes_to_write,
  1034. offset, &bytes_written, iov, n_iov,
  1035. 1);
  1036. if (rc || bytes_written < bytes_to_write) {
  1037. cERROR(1,("CIFSSMBWrite2 returned %d, written = %x",
  1038. rc, bytes_written));
  1039. set_bit(AS_EIO, &mapping->flags);
  1040. SetPageError(page);
  1041. }
  1042. for (i = 0; i < n_iov; i++) {
  1043. page = pvec.pages[first + i];
  1044. kunmap(page);
  1045. unlock_page(page);
  1046. page_cache_release(page);
  1047. }
  1048. if ((wbc->nr_to_write -= n_iov) <= 0)
  1049. done = 1;
  1050. index = next;
  1051. }
  1052. pagevec_release(&pvec);
  1053. }
  1054. if (!scanned && !done) {
  1055. /*
  1056. * We hit the last page and there is more work to be done: wrap
  1057. * back to the start of the file
  1058. */
  1059. scanned = 1;
  1060. index = 0;
  1061. goto retry;
  1062. }
  1063. if (!is_range)
  1064. mapping->writeback_index = index;
  1065. FreeXid(xid);
  1066. return rc;
  1067. }
  1068. #endif
  1069. static int cifs_writepage(struct page* page, struct writeback_control *wbc)
  1070. {
  1071. int rc = -EFAULT;
  1072. int xid;
  1073. xid = GetXid();
  1074. /* BB add check for wbc flags */
  1075. page_cache_get(page);
  1076. if (!PageUptodate(page)) {
  1077. cFYI(1, ("ppw - page not up to date"));
  1078. }
  1079. rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE);
  1080. SetPageUptodate(page); /* BB add check for error and Clearuptodate? */
  1081. unlock_page(page);
  1082. page_cache_release(page);
  1083. FreeXid(xid);
  1084. return rc;
  1085. }
  1086. static int cifs_commit_write(struct file *file, struct page *page,
  1087. unsigned offset, unsigned to)
  1088. {
  1089. int xid;
  1090. int rc = 0;
  1091. struct inode *inode = page->mapping->host;
  1092. loff_t position = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
  1093. char *page_data;
  1094. xid = GetXid();
  1095. cFYI(1, ("commit write for page %p up to position %lld for %d",
  1096. page, position, to));
  1097. if (position > inode->i_size) {
  1098. i_size_write(inode, position);
  1099. /* if (file->private_data == NULL) {
  1100. rc = -EBADF;
  1101. } else {
  1102. open_file = (struct cifsFileInfo *)file->private_data;
  1103. cifs_sb = CIFS_SB(inode->i_sb);
  1104. rc = -EAGAIN;
  1105. while (rc == -EAGAIN) {
  1106. if ((open_file->invalidHandle) &&
  1107. (!open_file->closePend)) {
  1108. rc = cifs_reopen_file(
  1109. file->f_dentry->d_inode, file);
  1110. if (rc != 0)
  1111. break;
  1112. }
  1113. if (!open_file->closePend) {
  1114. rc = CIFSSMBSetFileSize(xid,
  1115. cifs_sb->tcon, position,
  1116. open_file->netfid,
  1117. open_file->pid, FALSE);
  1118. } else {
  1119. rc = -EBADF;
  1120. break;
  1121. }
  1122. }
  1123. cFYI(1, (" SetEOF (commit write) rc = %d", rc));
  1124. } */
  1125. }
  1126. if (!PageUptodate(page)) {
  1127. position = ((loff_t)page->index << PAGE_CACHE_SHIFT) + offset;
  1128. /* can not rely on (or let) writepage write this data */
  1129. if (to < offset) {
  1130. cFYI(1, ("Illegal offsets, can not copy from %d to %d",
  1131. offset, to));
  1132. FreeXid(xid);
  1133. return rc;
  1134. }
  1135. /* this is probably better than directly calling
  1136. partialpage_write since in this function the file handle is
  1137. known which we might as well leverage */
  1138. /* BB check if anything else missing out of ppw
  1139. such as updating last write time */
  1140. page_data = kmap(page);
  1141. rc = cifs_write(file, page_data + offset, to-offset,
  1142. &position);
  1143. if (rc > 0)
  1144. rc = 0;
  1145. /* else if (rc < 0) should we set writebehind rc? */
  1146. kunmap(page);
  1147. } else {
  1148. set_page_dirty(page);
  1149. }
  1150. FreeXid(xid);
  1151. return rc;
  1152. }
  1153. int cifs_fsync(struct file *file, struct dentry *dentry, int datasync)
  1154. {
  1155. int xid;
  1156. int rc = 0;
  1157. struct inode *inode = file->f_dentry->d_inode;
  1158. xid = GetXid();
  1159. cFYI(1, ("Sync file - name: %s datasync: 0x%x ",
  1160. dentry->d_name.name, datasync));
  1161. rc = filemap_fdatawrite(inode->i_mapping);
  1162. if (rc == 0)
  1163. CIFS_I(inode)->write_behind_rc = 0;
  1164. FreeXid(xid);
  1165. return rc;
  1166. }
  1167. /* static int cifs_sync_page(struct page *page)
  1168. {
  1169. struct address_space *mapping;
  1170. struct inode *inode;
  1171. unsigned long index = page->index;
  1172. unsigned int rpages = 0;
  1173. int rc = 0;
  1174. cFYI(1, ("sync page %p",page));
  1175. mapping = page->mapping;
  1176. if (!mapping)
  1177. return 0;
  1178. inode = mapping->host;
  1179. if (!inode)
  1180. return 0; */
  1181. /* fill in rpages then
  1182. result = cifs_pagein_inode(inode, index, rpages); */ /* BB finish */
  1183. /* cFYI(1, ("rpages is %d for sync page of Index %ld ", rpages, index));
  1184. if (rc < 0)
  1185. return rc;
  1186. return 0;
  1187. } */
  1188. /*
  1189. * As file closes, flush all cached write data for this inode checking
  1190. * for write behind errors.
  1191. */
  1192. int cifs_flush(struct file *file)
  1193. {
  1194. struct inode * inode = file->f_dentry->d_inode;
  1195. int rc = 0;
  1196. /* Rather than do the steps manually:
  1197. lock the inode for writing
  1198. loop through pages looking for write behind data (dirty pages)
  1199. coalesce into contiguous 16K (or smaller) chunks to write to server
  1200. send to server (prefer in parallel)
  1201. deal with writebehind errors
  1202. unlock inode for writing
  1203. filemapfdatawrite appears easier for the time being */
  1204. rc = filemap_fdatawrite(inode->i_mapping);
  1205. if (!rc) /* reset wb rc if we were able to write out dirty pages */
  1206. CIFS_I(inode)->write_behind_rc = 0;
  1207. cFYI(1, ("Flush inode %p file %p rc %d",inode,file,rc));
  1208. return rc;
  1209. }
  1210. ssize_t cifs_user_read(struct file *file, char __user *read_data,
  1211. size_t read_size, loff_t *poffset)
  1212. {
  1213. int rc = -EACCES;
  1214. unsigned int bytes_read = 0;
  1215. unsigned int total_read = 0;
  1216. unsigned int current_read_size;
  1217. struct cifs_sb_info *cifs_sb;
  1218. struct cifsTconInfo *pTcon;
  1219. int xid;
  1220. struct cifsFileInfo *open_file;
  1221. char *smb_read_data;
  1222. char __user *current_offset;
  1223. struct smb_com_read_rsp *pSMBr;
  1224. xid = GetXid();
  1225. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  1226. pTcon = cifs_sb->tcon;
  1227. if (file->private_data == NULL) {
  1228. FreeXid(xid);
  1229. return -EBADF;
  1230. }
  1231. open_file = (struct cifsFileInfo *)file->private_data;
  1232. if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
  1233. cFYI(1, ("attempting read on write only file instance"));
  1234. }
  1235. for (total_read = 0, current_offset = read_data;
  1236. read_size > total_read;
  1237. total_read += bytes_read, current_offset += bytes_read) {
  1238. current_read_size = min_t(const int, read_size - total_read,
  1239. cifs_sb->rsize);
  1240. rc = -EAGAIN;
  1241. smb_read_data = NULL;
  1242. while (rc == -EAGAIN) {
  1243. if ((open_file->invalidHandle) &&
  1244. (!open_file->closePend)) {
  1245. rc = cifs_reopen_file(file->f_dentry->d_inode,
  1246. file, TRUE);
  1247. if (rc != 0)
  1248. break;
  1249. }
  1250. rc = CIFSSMBRead(xid, pTcon,
  1251. open_file->netfid,
  1252. current_read_size, *poffset,
  1253. &bytes_read, &smb_read_data);
  1254. pSMBr = (struct smb_com_read_rsp *)smb_read_data;
  1255. if (copy_to_user(current_offset,
  1256. smb_read_data + 4 /* RFC1001 hdr */
  1257. + le16_to_cpu(pSMBr->DataOffset),
  1258. bytes_read)) {
  1259. rc = -EFAULT;
  1260. FreeXid(xid);
  1261. return rc;
  1262. }
  1263. if (smb_read_data) {
  1264. cifs_buf_release(smb_read_data);
  1265. smb_read_data = NULL;
  1266. }
  1267. }
  1268. if (rc || (bytes_read == 0)) {
  1269. if (total_read) {
  1270. break;
  1271. } else {
  1272. FreeXid(xid);
  1273. return rc;
  1274. }
  1275. } else {
  1276. cifs_stats_bytes_read(pTcon, bytes_read);
  1277. *poffset += bytes_read;
  1278. }
  1279. }
  1280. FreeXid(xid);
  1281. return total_read;
  1282. }
  1283. static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
  1284. loff_t *poffset)
  1285. {
  1286. int rc = -EACCES;
  1287. unsigned int bytes_read = 0;
  1288. unsigned int total_read;
  1289. unsigned int current_read_size;
  1290. struct cifs_sb_info *cifs_sb;
  1291. struct cifsTconInfo *pTcon;
  1292. int xid;
  1293. char *current_offset;
  1294. struct cifsFileInfo *open_file;
  1295. xid = GetXid();
  1296. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  1297. pTcon = cifs_sb->tcon;
  1298. if (file->private_data == NULL) {
  1299. FreeXid(xid);
  1300. return -EBADF;
  1301. }
  1302. open_file = (struct cifsFileInfo *)file->private_data;
  1303. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  1304. cFYI(1, ("attempting read on write only file instance"));
  1305. for (total_read = 0, current_offset = read_data;
  1306. read_size > total_read;
  1307. total_read += bytes_read, current_offset += bytes_read) {
  1308. current_read_size = min_t(const int, read_size - total_read,
  1309. cifs_sb->rsize);
  1310. /* For windows me and 9x we do not want to request more
  1311. than it negotiated since it will refuse the read then */
  1312. if((pTcon->ses) &&
  1313. !(pTcon->ses->capabilities & CAP_LARGE_FILES)) {
  1314. current_read_size = min_t(const int, current_read_size,
  1315. pTcon->ses->server->maxBuf - 128);
  1316. }
  1317. rc = -EAGAIN;
  1318. while (rc == -EAGAIN) {
  1319. if ((open_file->invalidHandle) &&
  1320. (!open_file->closePend)) {
  1321. rc = cifs_reopen_file(file->f_dentry->d_inode,
  1322. file, TRUE);
  1323. if (rc != 0)
  1324. break;
  1325. }
  1326. rc = CIFSSMBRead(xid, pTcon,
  1327. open_file->netfid,
  1328. current_read_size, *poffset,
  1329. &bytes_read, &current_offset);
  1330. }
  1331. if (rc || (bytes_read == 0)) {
  1332. if (total_read) {
  1333. break;
  1334. } else {
  1335. FreeXid(xid);
  1336. return rc;
  1337. }
  1338. } else {
  1339. cifs_stats_bytes_read(pTcon, total_read);
  1340. *poffset += bytes_read;
  1341. }
  1342. }
  1343. FreeXid(xid);
  1344. return total_read;
  1345. }
  1346. int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
  1347. {
  1348. struct dentry *dentry = file->f_dentry;
  1349. int rc, xid;
  1350. xid = GetXid();
  1351. rc = cifs_revalidate(dentry);
  1352. if (rc) {
  1353. cFYI(1, ("Validation prior to mmap failed, error=%d", rc));
  1354. FreeXid(xid);
  1355. return rc;
  1356. }
  1357. rc = generic_file_mmap(file, vma);
  1358. FreeXid(xid);
  1359. return rc;
  1360. }
  1361. static void cifs_copy_cache_pages(struct address_space *mapping,
  1362. struct list_head *pages, int bytes_read, char *data,
  1363. struct pagevec *plru_pvec)
  1364. {
  1365. struct page *page;
  1366. char *target;
  1367. while (bytes_read > 0) {
  1368. if (list_empty(pages))
  1369. break;
  1370. page = list_entry(pages->prev, struct page, lru);
  1371. list_del(&page->lru);
  1372. if (add_to_page_cache(page, mapping, page->index,
  1373. GFP_KERNEL)) {
  1374. page_cache_release(page);
  1375. cFYI(1, ("Add page cache failed"));
  1376. data += PAGE_CACHE_SIZE;
  1377. bytes_read -= PAGE_CACHE_SIZE;
  1378. continue;
  1379. }
  1380. target = kmap_atomic(page,KM_USER0);
  1381. if (PAGE_CACHE_SIZE > bytes_read) {
  1382. memcpy(target, data, bytes_read);
  1383. /* zero the tail end of this partial page */
  1384. memset(target + bytes_read, 0,
  1385. PAGE_CACHE_SIZE - bytes_read);
  1386. bytes_read = 0;
  1387. } else {
  1388. memcpy(target, data, PAGE_CACHE_SIZE);
  1389. bytes_read -= PAGE_CACHE_SIZE;
  1390. }
  1391. kunmap_atomic(target, KM_USER0);
  1392. flush_dcache_page(page);
  1393. SetPageUptodate(page);
  1394. unlock_page(page);
  1395. if (!pagevec_add(plru_pvec, page))
  1396. __pagevec_lru_add(plru_pvec);
  1397. data += PAGE_CACHE_SIZE;
  1398. }
  1399. return;
  1400. }
  1401. static int cifs_readpages(struct file *file, struct address_space *mapping,
  1402. struct list_head *page_list, unsigned num_pages)
  1403. {
  1404. int rc = -EACCES;
  1405. int xid;
  1406. loff_t offset;
  1407. struct page *page;
  1408. struct cifs_sb_info *cifs_sb;
  1409. struct cifsTconInfo *pTcon;
  1410. int bytes_read = 0;
  1411. unsigned int read_size,i;
  1412. char *smb_read_data = NULL;
  1413. struct smb_com_read_rsp *pSMBr;
  1414. struct pagevec lru_pvec;
  1415. struct cifsFileInfo *open_file;
  1416. xid = GetXid();
  1417. if (file->private_data == NULL) {
  1418. FreeXid(xid);
  1419. return -EBADF;
  1420. }
  1421. open_file = (struct cifsFileInfo *)file->private_data;
  1422. cifs_sb = CIFS_SB(file->f_dentry->d_sb);
  1423. pTcon = cifs_sb->tcon;
  1424. pagevec_init(&lru_pvec, 0);
  1425. for (i = 0; i < num_pages; ) {
  1426. unsigned contig_pages;
  1427. struct page *tmp_page;
  1428. unsigned long expected_index;
  1429. if (list_empty(page_list))
  1430. break;
  1431. page = list_entry(page_list->prev, struct page, lru);
  1432. offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1433. /* count adjacent pages that we will read into */
  1434. contig_pages = 0;
  1435. expected_index =
  1436. list_entry(page_list->prev, struct page, lru)->index;
  1437. list_for_each_entry_reverse(tmp_page,page_list,lru) {
  1438. if (tmp_page->index == expected_index) {
  1439. contig_pages++;
  1440. expected_index++;
  1441. } else
  1442. break;
  1443. }
  1444. if (contig_pages + i > num_pages)
  1445. contig_pages = num_pages - i;
  1446. /* for reads over a certain size could initiate async
  1447. read ahead */
  1448. read_size = contig_pages * PAGE_CACHE_SIZE;
  1449. /* Read size needs to be in multiples of one page */
  1450. read_size = min_t(const unsigned int, read_size,
  1451. cifs_sb->rsize & PAGE_CACHE_MASK);
  1452. rc = -EAGAIN;
  1453. while (rc == -EAGAIN) {
  1454. if ((open_file->invalidHandle) &&
  1455. (!open_file->closePend)) {
  1456. rc = cifs_reopen_file(file->f_dentry->d_inode,
  1457. file, TRUE);
  1458. if (rc != 0)
  1459. break;
  1460. }
  1461. rc = CIFSSMBRead(xid, pTcon,
  1462. open_file->netfid,
  1463. read_size, offset,
  1464. &bytes_read, &smb_read_data);
  1465. /* BB more RC checks ? */
  1466. if (rc== -EAGAIN) {
  1467. if (smb_read_data) {
  1468. cifs_buf_release(smb_read_data);
  1469. smb_read_data = NULL;
  1470. }
  1471. }
  1472. }
  1473. if ((rc < 0) || (smb_read_data == NULL)) {
  1474. cFYI(1, ("Read error in readpages: %d", rc));
  1475. /* clean up remaing pages off list */
  1476. while (!list_empty(page_list) && (i < num_pages)) {
  1477. page = list_entry(page_list->prev, struct page,
  1478. lru);
  1479. list_del(&page->lru);
  1480. page_cache_release(page);
  1481. }
  1482. break;
  1483. } else if (bytes_read > 0) {
  1484. pSMBr = (struct smb_com_read_rsp *)smb_read_data;
  1485. cifs_copy_cache_pages(mapping, page_list, bytes_read,
  1486. smb_read_data + 4 /* RFC1001 hdr */ +
  1487. le16_to_cpu(pSMBr->DataOffset), &lru_pvec);
  1488. i += bytes_read >> PAGE_CACHE_SHIFT;
  1489. cifs_stats_bytes_read(pTcon, bytes_read);
  1490. if ((int)(bytes_read & PAGE_CACHE_MASK) != bytes_read) {
  1491. i++; /* account for partial page */
  1492. /* server copy of file can have smaller size
  1493. than client */
  1494. /* BB do we need to verify this common case ?
  1495. this case is ok - if we are at server EOF
  1496. we will hit it on next read */
  1497. /* while (!list_empty(page_list) && (i < num_pages)) {
  1498. page = list_entry(page_list->prev,
  1499. struct page, list);
  1500. list_del(&page->list);
  1501. page_cache_release(page);
  1502. }
  1503. break; */
  1504. }
  1505. } else {
  1506. cFYI(1, ("No bytes read (%d) at offset %lld . "
  1507. "Cleaning remaining pages from readahead list",
  1508. bytes_read, offset));
  1509. /* BB turn off caching and do new lookup on
  1510. file size at server? */
  1511. while (!list_empty(page_list) && (i < num_pages)) {
  1512. page = list_entry(page_list->prev, struct page,
  1513. lru);
  1514. list_del(&page->lru);
  1515. /* BB removeme - replace with zero of page? */
  1516. page_cache_release(page);
  1517. }
  1518. break;
  1519. }
  1520. if (smb_read_data) {
  1521. cifs_buf_release(smb_read_data);
  1522. smb_read_data = NULL;
  1523. }
  1524. bytes_read = 0;
  1525. }
  1526. pagevec_lru_add(&lru_pvec);
  1527. /* need to free smb_read_data buf before exit */
  1528. if (smb_read_data) {
  1529. cifs_buf_release(smb_read_data);
  1530. smb_read_data = NULL;
  1531. }
  1532. FreeXid(xid);
  1533. return rc;
  1534. }
  1535. static int cifs_readpage_worker(struct file *file, struct page *page,
  1536. loff_t *poffset)
  1537. {
  1538. char *read_data;
  1539. int rc;
  1540. page_cache_get(page);
  1541. read_data = kmap(page);
  1542. /* for reads over a certain size could initiate async read ahead */
  1543. rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset);
  1544. if (rc < 0)
  1545. goto io_error;
  1546. else
  1547. cFYI(1, ("Bytes read %d ",rc));
  1548. file->f_dentry->d_inode->i_atime =
  1549. current_fs_time(file->f_dentry->d_inode->i_sb);
  1550. if (PAGE_CACHE_SIZE > rc)
  1551. memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc);
  1552. flush_dcache_page(page);
  1553. SetPageUptodate(page);
  1554. rc = 0;
  1555. io_error:
  1556. kunmap(page);
  1557. page_cache_release(page);
  1558. return rc;
  1559. }
  1560. static int cifs_readpage(struct file *file, struct page *page)
  1561. {
  1562. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1563. int rc = -EACCES;
  1564. int xid;
  1565. xid = GetXid();
  1566. if (file->private_data == NULL) {
  1567. FreeXid(xid);
  1568. return -EBADF;
  1569. }
  1570. cFYI(1, ("readpage %p at offset %d 0x%x\n",
  1571. page, (int)offset, (int)offset));
  1572. rc = cifs_readpage_worker(file, page, &offset);
  1573. unlock_page(page);
  1574. FreeXid(xid);
  1575. return rc;
  1576. }
  1577. /* We do not want to update the file size from server for inodes
  1578. open for write - to avoid races with writepage extending
  1579. the file - in the future we could consider allowing
  1580. refreshing the inode only on increases in the file size
  1581. but this is tricky to do without racing with writebehind
  1582. page caching in the current Linux kernel design */
  1583. int is_size_safe_to_change(struct cifsInodeInfo *cifsInode)
  1584. {
  1585. if (cifsInode && find_writable_file(cifsInode))
  1586. return 0;
  1587. else
  1588. return 1;
  1589. }
  1590. static int cifs_prepare_write(struct file *file, struct page *page,
  1591. unsigned from, unsigned to)
  1592. {
  1593. int rc = 0;
  1594. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1595. cFYI(1, ("prepare write for page %p from %d to %d",page,from,to));
  1596. if (!PageUptodate(page)) {
  1597. /* if (to - from != PAGE_CACHE_SIZE) {
  1598. void *kaddr = kmap_atomic(page, KM_USER0);
  1599. memset(kaddr, 0, from);
  1600. memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
  1601. flush_dcache_page(page);
  1602. kunmap_atomic(kaddr, KM_USER0);
  1603. } */
  1604. /* If we are writing a full page it will be up to date,
  1605. no need to read from the server */
  1606. if ((to == PAGE_CACHE_SIZE) && (from == 0))
  1607. SetPageUptodate(page);
  1608. /* might as well read a page, it is fast enough */
  1609. if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
  1610. rc = cifs_readpage_worker(file, page, &offset);
  1611. } else {
  1612. /* should we try using another file handle if there is one -
  1613. how would we lock it to prevent close of that handle
  1614. racing with this read?
  1615. In any case this will be written out by commit_write */
  1616. }
  1617. }
  1618. /* BB should we pass any errors back?
  1619. e.g. if we do not have read access to the file */
  1620. return 0;
  1621. }
  1622. struct address_space_operations cifs_addr_ops = {
  1623. .readpage = cifs_readpage,
  1624. .readpages = cifs_readpages,
  1625. .writepage = cifs_writepage,
  1626. #ifdef CONFIG_CIFS_EXPERIMENTAL
  1627. .writepages = cifs_writepages,
  1628. #endif
  1629. .prepare_write = cifs_prepare_write,
  1630. .commit_write = cifs_commit_write,
  1631. .set_page_dirty = __set_page_dirty_nobuffers,
  1632. /* .sync_page = cifs_sync_page, */
  1633. /* .direct_IO = */
  1634. };