file.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342
  1. /*
  2. * fs/cifs/file.c
  3. *
  4. * vfs operations that deal with files
  5. *
  6. * Copyright (C) International Business Machines Corp., 2002,2007
  7. * Author(s): Steve French (sfrench@us.ibm.com)
  8. * Jeremy Allison (jra@samba.org)
  9. *
  10. * This library is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published
  12. * by the Free Software Foundation; either version 2.1 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  18. * the GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/fs.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/stat.h>
  27. #include <linux/fcntl.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/pagevec.h>
  30. #include <linux/writeback.h>
  31. #include <linux/task_io_accounting_ops.h>
  32. #include <linux/delay.h>
  33. #include <asm/div64.h>
  34. #include "cifsfs.h"
  35. #include "cifspdu.h"
  36. #include "cifsglob.h"
  37. #include "cifsproto.h"
  38. #include "cifs_unicode.h"
  39. #include "cifs_debug.h"
  40. #include "cifs_fs_sb.h"
  41. static inline struct cifsFileInfo *cifs_init_private(
  42. struct cifsFileInfo *private_data, struct inode *inode,
  43. struct file *file, __u16 netfid)
  44. {
  45. memset(private_data, 0, sizeof(struct cifsFileInfo));
  46. private_data->netfid = netfid;
  47. private_data->pid = current->tgid;
  48. mutex_init(&private_data->fh_mutex);
  49. mutex_init(&private_data->lock_mutex);
  50. INIT_LIST_HEAD(&private_data->llist);
  51. private_data->pfile = file; /* needed for writepage */
  52. private_data->pInode = inode;
  53. private_data->invalidHandle = false;
  54. private_data->closePend = false;
  55. /* we have to track num writers to the inode, since writepages
  56. does not tell us which handle the write is for so there can
  57. be a close (overlapping with write) of the filehandle that
  58. cifs_writepages chose to use */
  59. atomic_set(&private_data->wrtPending, 0);
  60. return private_data;
  61. }
  62. static inline int cifs_convert_flags(unsigned int flags)
  63. {
  64. if ((flags & O_ACCMODE) == O_RDONLY)
  65. return GENERIC_READ;
  66. else if ((flags & O_ACCMODE) == O_WRONLY)
  67. return GENERIC_WRITE;
  68. else if ((flags & O_ACCMODE) == O_RDWR) {
  69. /* GENERIC_ALL is too much permission to request
  70. can cause unnecessary access denied on create */
  71. /* return GENERIC_ALL; */
  72. return (GENERIC_READ | GENERIC_WRITE);
  73. }
  74. return (READ_CONTROL | FILE_WRITE_ATTRIBUTES | FILE_READ_ATTRIBUTES |
  75. FILE_WRITE_EA | FILE_APPEND_DATA | FILE_WRITE_DATA |
  76. FILE_READ_DATA);
  77. }
  78. static inline fmode_t cifs_posix_convert_flags(unsigned int flags)
  79. {
  80. fmode_t posix_flags = 0;
  81. if ((flags & O_ACCMODE) == O_RDONLY)
  82. posix_flags = FMODE_READ;
  83. else if ((flags & O_ACCMODE) == O_WRONLY)
  84. posix_flags = FMODE_WRITE;
  85. else if ((flags & O_ACCMODE) == O_RDWR) {
  86. /* GENERIC_ALL is too much permission to request
  87. can cause unnecessary access denied on create */
  88. /* return GENERIC_ALL; */
  89. posix_flags = FMODE_READ | FMODE_WRITE;
  90. }
  91. /* can not map O_CREAT or O_EXCL or O_TRUNC flags when
  92. reopening a file. They had their effect on the original open */
  93. if (flags & O_APPEND)
  94. posix_flags |= (fmode_t)O_APPEND;
  95. if (flags & O_SYNC)
  96. posix_flags |= (fmode_t)O_SYNC;
  97. if (flags & O_DIRECTORY)
  98. posix_flags |= (fmode_t)O_DIRECTORY;
  99. if (flags & O_NOFOLLOW)
  100. posix_flags |= (fmode_t)O_NOFOLLOW;
  101. if (flags & O_DIRECT)
  102. posix_flags |= (fmode_t)O_DIRECT;
  103. return posix_flags;
  104. }
  105. static inline int cifs_get_disposition(unsigned int flags)
  106. {
  107. if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  108. return FILE_CREATE;
  109. else if ((flags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
  110. return FILE_OVERWRITE_IF;
  111. else if ((flags & O_CREAT) == O_CREAT)
  112. return FILE_OPEN_IF;
  113. else if ((flags & O_TRUNC) == O_TRUNC)
  114. return FILE_OVERWRITE;
  115. else
  116. return FILE_OPEN;
  117. }
  118. /* all arguments to this function must be checked for validity in caller */
  119. static inline int cifs_posix_open_inode_helper(struct inode *inode,
  120. struct file *file, struct cifsInodeInfo *pCifsInode,
  121. struct cifsFileInfo *pCifsFile, int oplock, u16 netfid)
  122. {
  123. file->private_data = kmalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  124. if (file->private_data == NULL)
  125. return -ENOMEM;
  126. pCifsFile = cifs_init_private(file->private_data, inode, file, netfid);
  127. write_lock(&GlobalSMBSeslock);
  128. pCifsInode = CIFS_I(file->f_path.dentry->d_inode);
  129. if (pCifsInode == NULL) {
  130. write_unlock(&GlobalSMBSeslock);
  131. return -EINVAL;
  132. }
  133. if (pCifsInode->clientCanCacheRead) {
  134. /* we have the inode open somewhere else
  135. no need to discard cache data */
  136. goto psx_client_can_cache;
  137. }
  138. /* BB FIXME need to fix this check to move it earlier into posix_open
  139. BB fIX following section BB FIXME */
  140. /* if not oplocked, invalidate inode pages if mtime or file
  141. size changed */
  142. /* temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime));
  143. if (timespec_equal(&file->f_path.dentry->d_inode->i_mtime, &temp) &&
  144. (file->f_path.dentry->d_inode->i_size ==
  145. (loff_t)le64_to_cpu(buf->EndOfFile))) {
  146. cFYI(1, ("inode unchanged on server"));
  147. } else {
  148. if (file->f_path.dentry->d_inode->i_mapping) {
  149. rc = filemap_write_and_wait(file->f_path.dentry->d_inode->i_mapping);
  150. if (rc != 0)
  151. CIFS_I(file->f_path.dentry->d_inode)->write_behind_rc = rc;
  152. }
  153. cFYI(1, ("invalidating remote inode since open detected it "
  154. "changed"));
  155. invalidate_remote_inode(file->f_path.dentry->d_inode);
  156. } */
  157. psx_client_can_cache:
  158. if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  159. pCifsInode->clientCanCacheAll = true;
  160. pCifsInode->clientCanCacheRead = true;
  161. cFYI(1, ("Exclusive Oplock granted on inode %p",
  162. file->f_path.dentry->d_inode));
  163. } else if ((oplock & 0xF) == OPLOCK_READ)
  164. pCifsInode->clientCanCacheRead = true;
  165. /* will have to change the unlock if we reenable the
  166. filemap_fdatawrite (which does not seem necessary */
  167. write_unlock(&GlobalSMBSeslock);
  168. return 0;
  169. }
  170. /* all arguments to this function must be checked for validity in caller */
  171. static inline int cifs_open_inode_helper(struct inode *inode, struct file *file,
  172. struct cifsInodeInfo *pCifsInode, struct cifsFileInfo *pCifsFile,
  173. struct cifsTconInfo *pTcon, int *oplock, FILE_ALL_INFO *buf,
  174. char *full_path, int xid)
  175. {
  176. struct timespec temp;
  177. int rc;
  178. /* want handles we can use to read with first
  179. in the list so we do not have to walk the
  180. list to search for one in write_begin */
  181. if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
  182. list_add_tail(&pCifsFile->flist,
  183. &pCifsInode->openFileList);
  184. } else {
  185. list_add(&pCifsFile->flist,
  186. &pCifsInode->openFileList);
  187. }
  188. write_unlock(&GlobalSMBSeslock);
  189. if (pCifsInode->clientCanCacheRead) {
  190. /* we have the inode open somewhere else
  191. no need to discard cache data */
  192. goto client_can_cache;
  193. }
  194. /* BB need same check in cifs_create too? */
  195. /* if not oplocked, invalidate inode pages if mtime or file
  196. size changed */
  197. temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime));
  198. if (timespec_equal(&file->f_path.dentry->d_inode->i_mtime, &temp) &&
  199. (file->f_path.dentry->d_inode->i_size ==
  200. (loff_t)le64_to_cpu(buf->EndOfFile))) {
  201. cFYI(1, ("inode unchanged on server"));
  202. } else {
  203. if (file->f_path.dentry->d_inode->i_mapping) {
  204. /* BB no need to lock inode until after invalidate
  205. since namei code should already have it locked? */
  206. rc = filemap_write_and_wait(file->f_path.dentry->d_inode->i_mapping);
  207. if (rc != 0)
  208. CIFS_I(file->f_path.dentry->d_inode)->write_behind_rc = rc;
  209. }
  210. cFYI(1, ("invalidating remote inode since open detected it "
  211. "changed"));
  212. invalidate_remote_inode(file->f_path.dentry->d_inode);
  213. }
  214. client_can_cache:
  215. if (pTcon->unix_ext)
  216. rc = cifs_get_inode_info_unix(&file->f_path.dentry->d_inode,
  217. full_path, inode->i_sb, xid);
  218. else
  219. rc = cifs_get_inode_info(&file->f_path.dentry->d_inode,
  220. full_path, buf, inode->i_sb, xid, NULL);
  221. if ((*oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  222. pCifsInode->clientCanCacheAll = true;
  223. pCifsInode->clientCanCacheRead = true;
  224. cFYI(1, ("Exclusive Oplock granted on inode %p",
  225. file->f_path.dentry->d_inode));
  226. } else if ((*oplock & 0xF) == OPLOCK_READ)
  227. pCifsInode->clientCanCacheRead = true;
  228. return rc;
  229. }
  230. int cifs_open(struct inode *inode, struct file *file)
  231. {
  232. int rc = -EACCES;
  233. int xid, oplock;
  234. struct cifs_sb_info *cifs_sb;
  235. struct cifsTconInfo *tcon;
  236. struct cifsFileInfo *pCifsFile;
  237. struct cifsInodeInfo *pCifsInode;
  238. struct list_head *tmp;
  239. char *full_path = NULL;
  240. int desiredAccess;
  241. int disposition;
  242. __u16 netfid;
  243. FILE_ALL_INFO *buf = NULL;
  244. xid = GetXid();
  245. cifs_sb = CIFS_SB(inode->i_sb);
  246. tcon = cifs_sb->tcon;
  247. /* search inode for this file and fill in file->private_data */
  248. pCifsInode = CIFS_I(file->f_path.dentry->d_inode);
  249. read_lock(&GlobalSMBSeslock);
  250. list_for_each(tmp, &pCifsInode->openFileList) {
  251. pCifsFile = list_entry(tmp, struct cifsFileInfo,
  252. flist);
  253. if ((pCifsFile->pfile == NULL) &&
  254. (pCifsFile->pid == current->tgid)) {
  255. /* mode set in cifs_create */
  256. /* needed for writepage */
  257. pCifsFile->pfile = file;
  258. file->private_data = pCifsFile;
  259. break;
  260. }
  261. }
  262. read_unlock(&GlobalSMBSeslock);
  263. if (file->private_data != NULL) {
  264. rc = 0;
  265. FreeXid(xid);
  266. return rc;
  267. } else if ((file->f_flags & O_CREAT) && (file->f_flags & O_EXCL))
  268. cERROR(1, ("could not find file instance for "
  269. "new file %p", file));
  270. full_path = build_path_from_dentry(file->f_path.dentry);
  271. if (full_path == NULL) {
  272. FreeXid(xid);
  273. return -ENOMEM;
  274. }
  275. cFYI(1, ("inode = 0x%p file flags are 0x%x for %s",
  276. inode, file->f_flags, full_path));
  277. if (oplockEnabled)
  278. oplock = REQ_OPLOCK;
  279. else
  280. oplock = 0;
  281. if (!tcon->broken_posix_open && tcon->unix_ext &&
  282. (tcon->ses->capabilities & CAP_UNIX) &&
  283. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  284. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  285. int oflags = (int) cifs_posix_convert_flags(file->f_flags);
  286. /* can not refresh inode info since size could be stale */
  287. rc = cifs_posix_open(full_path, &inode, inode->i_sb,
  288. cifs_sb->mnt_file_mode /* ignored */,
  289. oflags, &oplock, &netfid, xid);
  290. if (rc == 0) {
  291. cFYI(1, ("posix open succeeded"));
  292. /* no need for special case handling of setting mode
  293. on read only files needed here */
  294. cifs_posix_open_inode_helper(inode, file, pCifsInode,
  295. pCifsFile, oplock, netfid);
  296. goto out;
  297. } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  298. if (tcon->ses->serverNOS)
  299. cERROR(1, ("server %s of type %s returned"
  300. " unexpected error on SMB posix open"
  301. ", disabling posix open support."
  302. " Check if server update available.",
  303. tcon->ses->serverName,
  304. tcon->ses->serverNOS));
  305. tcon->broken_posix_open = true;
  306. } else if ((rc != -EIO) && (rc != -EREMOTE) &&
  307. (rc != -EOPNOTSUPP)) /* path not found or net err */
  308. goto out;
  309. /* else fallthrough to retry open the old way on network i/o
  310. or DFS errors */
  311. }
  312. desiredAccess = cifs_convert_flags(file->f_flags);
  313. /*********************************************************************
  314. * open flag mapping table:
  315. *
  316. * POSIX Flag CIFS Disposition
  317. * ---------- ----------------
  318. * O_CREAT FILE_OPEN_IF
  319. * O_CREAT | O_EXCL FILE_CREATE
  320. * O_CREAT | O_TRUNC FILE_OVERWRITE_IF
  321. * O_TRUNC FILE_OVERWRITE
  322. * none of the above FILE_OPEN
  323. *
  324. * Note that there is not a direct match between disposition
  325. * FILE_SUPERSEDE (ie create whether or not file exists although
  326. * O_CREAT | O_TRUNC is similar but truncates the existing
  327. * file rather than creating a new file as FILE_SUPERSEDE does
  328. * (which uses the attributes / metadata passed in on open call)
  329. *?
  330. *? O_SYNC is a reasonable match to CIFS writethrough flag
  331. *? and the read write flags match reasonably. O_LARGEFILE
  332. *? is irrelevant because largefile support is always used
  333. *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY,
  334. * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation
  335. *********************************************************************/
  336. disposition = cifs_get_disposition(file->f_flags);
  337. /* BB pass O_SYNC flag through on file attributes .. BB */
  338. /* Also refresh inode by passing in file_info buf returned by SMBOpen
  339. and calling get_inode_info with returned buf (at least helps
  340. non-Unix server case) */
  341. /* BB we can not do this if this is the second open of a file
  342. and the first handle has writebehind data, we might be
  343. able to simply do a filemap_fdatawrite/filemap_fdatawait first */
  344. buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  345. if (!buf) {
  346. rc = -ENOMEM;
  347. goto out;
  348. }
  349. if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS)
  350. rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
  351. desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf,
  352. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
  353. & CIFS_MOUNT_MAP_SPECIAL_CHR);
  354. else
  355. rc = -EIO; /* no NT SMB support fall into legacy open below */
  356. if (rc == -EIO) {
  357. /* Old server, try legacy style OpenX */
  358. rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
  359. desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf,
  360. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags
  361. & CIFS_MOUNT_MAP_SPECIAL_CHR);
  362. }
  363. if (rc) {
  364. cFYI(1, ("cifs_open returned 0x%x", rc));
  365. goto out;
  366. }
  367. file->private_data =
  368. kmalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
  369. if (file->private_data == NULL) {
  370. rc = -ENOMEM;
  371. goto out;
  372. }
  373. pCifsFile = cifs_init_private(file->private_data, inode, file, netfid);
  374. write_lock(&GlobalSMBSeslock);
  375. list_add(&pCifsFile->tlist, &tcon->openFileList);
  376. pCifsInode = CIFS_I(file->f_path.dentry->d_inode);
  377. if (pCifsInode) {
  378. rc = cifs_open_inode_helper(inode, file, pCifsInode,
  379. pCifsFile, tcon,
  380. &oplock, buf, full_path, xid);
  381. } else {
  382. write_unlock(&GlobalSMBSeslock);
  383. }
  384. if (oplock & CIFS_CREATE_ACTION) {
  385. /* time to set mode which we can not set earlier due to
  386. problems creating new read-only files */
  387. if (tcon->unix_ext) {
  388. struct cifs_unix_set_info_args args = {
  389. .mode = inode->i_mode,
  390. .uid = NO_CHANGE_64,
  391. .gid = NO_CHANGE_64,
  392. .ctime = NO_CHANGE_64,
  393. .atime = NO_CHANGE_64,
  394. .mtime = NO_CHANGE_64,
  395. .device = 0,
  396. };
  397. CIFSSMBUnixSetInfo(xid, tcon, full_path, &args,
  398. cifs_sb->local_nls,
  399. cifs_sb->mnt_cifs_flags &
  400. CIFS_MOUNT_MAP_SPECIAL_CHR);
  401. }
  402. }
  403. out:
  404. kfree(buf);
  405. kfree(full_path);
  406. FreeXid(xid);
  407. return rc;
  408. }
  409. /* Try to reacquire byte range locks that were released when session */
  410. /* to server was lost */
  411. static int cifs_relock_file(struct cifsFileInfo *cifsFile)
  412. {
  413. int rc = 0;
  414. /* BB list all locks open on this file and relock */
  415. return rc;
  416. }
  417. static int cifs_reopen_file(struct file *file, bool can_flush)
  418. {
  419. int rc = -EACCES;
  420. int xid, oplock;
  421. struct cifs_sb_info *cifs_sb;
  422. struct cifsTconInfo *tcon;
  423. struct cifsFileInfo *pCifsFile;
  424. struct cifsInodeInfo *pCifsInode;
  425. struct inode *inode;
  426. char *full_path = NULL;
  427. int desiredAccess;
  428. int disposition = FILE_OPEN;
  429. __u16 netfid;
  430. if (file->private_data)
  431. pCifsFile = (struct cifsFileInfo *)file->private_data;
  432. else
  433. return -EBADF;
  434. xid = GetXid();
  435. mutex_unlock(&pCifsFile->fh_mutex);
  436. if (!pCifsFile->invalidHandle) {
  437. mutex_lock(&pCifsFile->fh_mutex);
  438. FreeXid(xid);
  439. return 0;
  440. }
  441. if (file->f_path.dentry == NULL) {
  442. cERROR(1, ("no valid name if dentry freed"));
  443. dump_stack();
  444. rc = -EBADF;
  445. goto reopen_error_exit;
  446. }
  447. inode = file->f_path.dentry->d_inode;
  448. if (inode == NULL) {
  449. cERROR(1, ("inode not valid"));
  450. dump_stack();
  451. rc = -EBADF;
  452. goto reopen_error_exit;
  453. }
  454. cifs_sb = CIFS_SB(inode->i_sb);
  455. tcon = cifs_sb->tcon;
  456. /* can not grab rename sem here because various ops, including
  457. those that already have the rename sem can end up causing writepage
  458. to get called and if the server was down that means we end up here,
  459. and we can never tell if the caller already has the rename_sem */
  460. full_path = build_path_from_dentry(file->f_path.dentry);
  461. if (full_path == NULL) {
  462. rc = -ENOMEM;
  463. reopen_error_exit:
  464. mutex_lock(&pCifsFile->fh_mutex);
  465. FreeXid(xid);
  466. return rc;
  467. }
  468. cFYI(1, ("inode = 0x%p file flags 0x%x for %s",
  469. inode, file->f_flags, full_path));
  470. if (oplockEnabled)
  471. oplock = REQ_OPLOCK;
  472. else
  473. oplock = 0;
  474. if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) &&
  475. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  476. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  477. int oflags = (int) cifs_posix_convert_flags(file->f_flags);
  478. /* can not refresh inode info since size could be stale */
  479. rc = cifs_posix_open(full_path, NULL, inode->i_sb,
  480. cifs_sb->mnt_file_mode /* ignored */,
  481. oflags, &oplock, &netfid, xid);
  482. if (rc == 0) {
  483. cFYI(1, ("posix reopen succeeded"));
  484. goto reopen_success;
  485. }
  486. /* fallthrough to retry open the old way on errors, especially
  487. in the reconnect path it is important to retry hard */
  488. }
  489. desiredAccess = cifs_convert_flags(file->f_flags);
  490. /* Can not refresh inode by passing in file_info buf to be returned
  491. by SMBOpen and then calling get_inode_info with returned buf
  492. since file might have write behind data that needs to be flushed
  493. and server version of file size can be stale. If we knew for sure
  494. that inode was not dirty locally we could do this */
  495. rc = CIFSSMBOpen(xid, tcon, full_path, disposition, desiredAccess,
  496. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  497. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  498. CIFS_MOUNT_MAP_SPECIAL_CHR);
  499. if (rc) {
  500. mutex_lock(&pCifsFile->fh_mutex);
  501. cFYI(1, ("cifs_open returned 0x%x", rc));
  502. cFYI(1, ("oplock: %d", oplock));
  503. } else {
  504. reopen_success:
  505. pCifsFile->netfid = netfid;
  506. pCifsFile->invalidHandle = false;
  507. mutex_lock(&pCifsFile->fh_mutex);
  508. pCifsInode = CIFS_I(inode);
  509. if (pCifsInode) {
  510. if (can_flush) {
  511. rc = filemap_write_and_wait(inode->i_mapping);
  512. if (rc != 0)
  513. CIFS_I(inode)->write_behind_rc = rc;
  514. /* temporarily disable caching while we
  515. go to server to get inode info */
  516. pCifsInode->clientCanCacheAll = false;
  517. pCifsInode->clientCanCacheRead = false;
  518. if (tcon->unix_ext)
  519. rc = cifs_get_inode_info_unix(&inode,
  520. full_path, inode->i_sb, xid);
  521. else
  522. rc = cifs_get_inode_info(&inode,
  523. full_path, NULL, inode->i_sb,
  524. xid, NULL);
  525. } /* else we are writing out data to server already
  526. and could deadlock if we tried to flush data, and
  527. since we do not know if we have data that would
  528. invalidate the current end of file on the server
  529. we can not go to the server to get the new inod
  530. info */
  531. if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
  532. pCifsInode->clientCanCacheAll = true;
  533. pCifsInode->clientCanCacheRead = true;
  534. cFYI(1, ("Exclusive Oplock granted on inode %p",
  535. file->f_path.dentry->d_inode));
  536. } else if ((oplock & 0xF) == OPLOCK_READ) {
  537. pCifsInode->clientCanCacheRead = true;
  538. pCifsInode->clientCanCacheAll = false;
  539. } else {
  540. pCifsInode->clientCanCacheRead = false;
  541. pCifsInode->clientCanCacheAll = false;
  542. }
  543. cifs_relock_file(pCifsFile);
  544. }
  545. }
  546. kfree(full_path);
  547. FreeXid(xid);
  548. return rc;
  549. }
  550. int cifs_close(struct inode *inode, struct file *file)
  551. {
  552. int rc = 0;
  553. int xid, timeout;
  554. struct cifs_sb_info *cifs_sb;
  555. struct cifsTconInfo *pTcon;
  556. struct cifsFileInfo *pSMBFile =
  557. (struct cifsFileInfo *)file->private_data;
  558. xid = GetXid();
  559. cifs_sb = CIFS_SB(inode->i_sb);
  560. pTcon = cifs_sb->tcon;
  561. if (pSMBFile) {
  562. struct cifsLockInfo *li, *tmp;
  563. write_lock(&GlobalSMBSeslock);
  564. pSMBFile->closePend = true;
  565. if (pTcon) {
  566. /* no sense reconnecting to close a file that is
  567. already closed */
  568. if (!pTcon->need_reconnect) {
  569. write_unlock(&GlobalSMBSeslock);
  570. timeout = 2;
  571. while ((atomic_read(&pSMBFile->wrtPending) != 0)
  572. && (timeout <= 2048)) {
  573. /* Give write a better chance to get to
  574. server ahead of the close. We do not
  575. want to add a wait_q here as it would
  576. increase the memory utilization as
  577. the struct would be in each open file,
  578. but this should give enough time to
  579. clear the socket */
  580. cFYI(DBG2,
  581. ("close delay, write pending"));
  582. msleep(timeout);
  583. timeout *= 4;
  584. }
  585. if (atomic_read(&pSMBFile->wrtPending))
  586. cERROR(1, ("close with pending write"));
  587. if (!pTcon->need_reconnect &&
  588. !pSMBFile->invalidHandle)
  589. rc = CIFSSMBClose(xid, pTcon,
  590. pSMBFile->netfid);
  591. } else
  592. write_unlock(&GlobalSMBSeslock);
  593. } else
  594. write_unlock(&GlobalSMBSeslock);
  595. /* Delete any outstanding lock records.
  596. We'll lose them when the file is closed anyway. */
  597. mutex_lock(&pSMBFile->lock_mutex);
  598. list_for_each_entry_safe(li, tmp, &pSMBFile->llist, llist) {
  599. list_del(&li->llist);
  600. kfree(li);
  601. }
  602. mutex_unlock(&pSMBFile->lock_mutex);
  603. write_lock(&GlobalSMBSeslock);
  604. list_del(&pSMBFile->flist);
  605. list_del(&pSMBFile->tlist);
  606. write_unlock(&GlobalSMBSeslock);
  607. timeout = 10;
  608. /* We waited above to give the SMBWrite a chance to issue
  609. on the wire (so we do not get SMBWrite returning EBADF
  610. if writepages is racing with close. Note that writepages
  611. does not specify a file handle, so it is possible for a file
  612. to be opened twice, and the application close the "wrong"
  613. file handle - in these cases we delay long enough to allow
  614. the SMBWrite to get on the wire before the SMB Close.
  615. We allow total wait here over 45 seconds, more than
  616. oplock break time, and more than enough to allow any write
  617. to complete on the server, or to time out on the client */
  618. while ((atomic_read(&pSMBFile->wrtPending) != 0)
  619. && (timeout <= 50000)) {
  620. cERROR(1, ("writes pending, delay free of handle"));
  621. msleep(timeout);
  622. timeout *= 8;
  623. }
  624. kfree(file->private_data);
  625. file->private_data = NULL;
  626. } else
  627. rc = -EBADF;
  628. read_lock(&GlobalSMBSeslock);
  629. if (list_empty(&(CIFS_I(inode)->openFileList))) {
  630. cFYI(1, ("closing last open instance for inode %p", inode));
  631. /* if the file is not open we do not know if we can cache info
  632. on this inode, much less write behind and read ahead */
  633. CIFS_I(inode)->clientCanCacheRead = false;
  634. CIFS_I(inode)->clientCanCacheAll = false;
  635. }
  636. read_unlock(&GlobalSMBSeslock);
  637. if ((rc == 0) && CIFS_I(inode)->write_behind_rc)
  638. rc = CIFS_I(inode)->write_behind_rc;
  639. FreeXid(xid);
  640. return rc;
  641. }
  642. int cifs_closedir(struct inode *inode, struct file *file)
  643. {
  644. int rc = 0;
  645. int xid;
  646. struct cifsFileInfo *pCFileStruct =
  647. (struct cifsFileInfo *)file->private_data;
  648. char *ptmp;
  649. cFYI(1, ("Closedir inode = 0x%p", inode));
  650. xid = GetXid();
  651. if (pCFileStruct) {
  652. struct cifsTconInfo *pTcon;
  653. struct cifs_sb_info *cifs_sb =
  654. CIFS_SB(file->f_path.dentry->d_sb);
  655. pTcon = cifs_sb->tcon;
  656. cFYI(1, ("Freeing private data in close dir"));
  657. write_lock(&GlobalSMBSeslock);
  658. if (!pCFileStruct->srch_inf.endOfSearch &&
  659. !pCFileStruct->invalidHandle) {
  660. pCFileStruct->invalidHandle = true;
  661. write_unlock(&GlobalSMBSeslock);
  662. rc = CIFSFindClose(xid, pTcon, pCFileStruct->netfid);
  663. cFYI(1, ("Closing uncompleted readdir with rc %d",
  664. rc));
  665. /* not much we can do if it fails anyway, ignore rc */
  666. rc = 0;
  667. } else
  668. write_unlock(&GlobalSMBSeslock);
  669. ptmp = pCFileStruct->srch_inf.ntwrk_buf_start;
  670. if (ptmp) {
  671. cFYI(1, ("closedir free smb buf in srch struct"));
  672. pCFileStruct->srch_inf.ntwrk_buf_start = NULL;
  673. if (pCFileStruct->srch_inf.smallBuf)
  674. cifs_small_buf_release(ptmp);
  675. else
  676. cifs_buf_release(ptmp);
  677. }
  678. kfree(file->private_data);
  679. file->private_data = NULL;
  680. }
  681. /* BB can we lock the filestruct while this is going on? */
  682. FreeXid(xid);
  683. return rc;
  684. }
  685. static int store_file_lock(struct cifsFileInfo *fid, __u64 len,
  686. __u64 offset, __u8 lockType)
  687. {
  688. struct cifsLockInfo *li =
  689. kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
  690. if (li == NULL)
  691. return -ENOMEM;
  692. li->offset = offset;
  693. li->length = len;
  694. li->type = lockType;
  695. mutex_lock(&fid->lock_mutex);
  696. list_add(&li->llist, &fid->llist);
  697. mutex_unlock(&fid->lock_mutex);
  698. return 0;
  699. }
  700. int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock)
  701. {
  702. int rc, xid;
  703. __u32 numLock = 0;
  704. __u32 numUnlock = 0;
  705. __u64 length;
  706. bool wait_flag = false;
  707. struct cifs_sb_info *cifs_sb;
  708. struct cifsTconInfo *tcon;
  709. __u16 netfid;
  710. __u8 lockType = LOCKING_ANDX_LARGE_FILES;
  711. bool posix_locking = 0;
  712. length = 1 + pfLock->fl_end - pfLock->fl_start;
  713. rc = -EACCES;
  714. xid = GetXid();
  715. cFYI(1, ("Lock parm: 0x%x flockflags: "
  716. "0x%x flocktype: 0x%x start: %lld end: %lld",
  717. cmd, pfLock->fl_flags, pfLock->fl_type, pfLock->fl_start,
  718. pfLock->fl_end));
  719. if (pfLock->fl_flags & FL_POSIX)
  720. cFYI(1, ("Posix"));
  721. if (pfLock->fl_flags & FL_FLOCK)
  722. cFYI(1, ("Flock"));
  723. if (pfLock->fl_flags & FL_SLEEP) {
  724. cFYI(1, ("Blocking lock"));
  725. wait_flag = true;
  726. }
  727. if (pfLock->fl_flags & FL_ACCESS)
  728. cFYI(1, ("Process suspended by mandatory locking - "
  729. "not implemented yet"));
  730. if (pfLock->fl_flags & FL_LEASE)
  731. cFYI(1, ("Lease on file - not implemented yet"));
  732. if (pfLock->fl_flags &
  733. (~(FL_POSIX | FL_FLOCK | FL_SLEEP | FL_ACCESS | FL_LEASE)))
  734. cFYI(1, ("Unknown lock flags 0x%x", pfLock->fl_flags));
  735. if (pfLock->fl_type == F_WRLCK) {
  736. cFYI(1, ("F_WRLCK "));
  737. numLock = 1;
  738. } else if (pfLock->fl_type == F_UNLCK) {
  739. cFYI(1, ("F_UNLCK"));
  740. numUnlock = 1;
  741. /* Check if unlock includes more than
  742. one lock range */
  743. } else if (pfLock->fl_type == F_RDLCK) {
  744. cFYI(1, ("F_RDLCK"));
  745. lockType |= LOCKING_ANDX_SHARED_LOCK;
  746. numLock = 1;
  747. } else if (pfLock->fl_type == F_EXLCK) {
  748. cFYI(1, ("F_EXLCK"));
  749. numLock = 1;
  750. } else if (pfLock->fl_type == F_SHLCK) {
  751. cFYI(1, ("F_SHLCK"));
  752. lockType |= LOCKING_ANDX_SHARED_LOCK;
  753. numLock = 1;
  754. } else
  755. cFYI(1, ("Unknown type of lock"));
  756. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  757. tcon = cifs_sb->tcon;
  758. if (file->private_data == NULL) {
  759. FreeXid(xid);
  760. return -EBADF;
  761. }
  762. netfid = ((struct cifsFileInfo *)file->private_data)->netfid;
  763. if ((tcon->ses->capabilities & CAP_UNIX) &&
  764. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  765. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  766. posix_locking = 1;
  767. /* BB add code here to normalize offset and length to
  768. account for negative length which we can not accept over the
  769. wire */
  770. if (IS_GETLK(cmd)) {
  771. if (posix_locking) {
  772. int posix_lock_type;
  773. if (lockType & LOCKING_ANDX_SHARED_LOCK)
  774. posix_lock_type = CIFS_RDLCK;
  775. else
  776. posix_lock_type = CIFS_WRLCK;
  777. rc = CIFSSMBPosixLock(xid, tcon, netfid, 1 /* get */,
  778. length, pfLock,
  779. posix_lock_type, wait_flag);
  780. FreeXid(xid);
  781. return rc;
  782. }
  783. /* BB we could chain these into one lock request BB */
  784. rc = CIFSSMBLock(xid, tcon, netfid, length, pfLock->fl_start,
  785. 0, 1, lockType, 0 /* wait flag */ );
  786. if (rc == 0) {
  787. rc = CIFSSMBLock(xid, tcon, netfid, length,
  788. pfLock->fl_start, 1 /* numUnlock */ ,
  789. 0 /* numLock */ , lockType,
  790. 0 /* wait flag */ );
  791. pfLock->fl_type = F_UNLCK;
  792. if (rc != 0)
  793. cERROR(1, ("Error unlocking previously locked "
  794. "range %d during test of lock", rc));
  795. rc = 0;
  796. } else {
  797. /* if rc == ERR_SHARING_VIOLATION ? */
  798. rc = 0; /* do not change lock type to unlock
  799. since range in use */
  800. }
  801. FreeXid(xid);
  802. return rc;
  803. }
  804. if (!numLock && !numUnlock) {
  805. /* if no lock or unlock then nothing
  806. to do since we do not know what it is */
  807. FreeXid(xid);
  808. return -EOPNOTSUPP;
  809. }
  810. if (posix_locking) {
  811. int posix_lock_type;
  812. if (lockType & LOCKING_ANDX_SHARED_LOCK)
  813. posix_lock_type = CIFS_RDLCK;
  814. else
  815. posix_lock_type = CIFS_WRLCK;
  816. if (numUnlock == 1)
  817. posix_lock_type = CIFS_UNLCK;
  818. rc = CIFSSMBPosixLock(xid, tcon, netfid, 0 /* set */,
  819. length, pfLock,
  820. posix_lock_type, wait_flag);
  821. } else {
  822. struct cifsFileInfo *fid =
  823. (struct cifsFileInfo *)file->private_data;
  824. if (numLock) {
  825. rc = CIFSSMBLock(xid, tcon, netfid, length,
  826. pfLock->fl_start,
  827. 0, numLock, lockType, wait_flag);
  828. if (rc == 0) {
  829. /* For Windows locks we must store them. */
  830. rc = store_file_lock(fid, length,
  831. pfLock->fl_start, lockType);
  832. }
  833. } else if (numUnlock) {
  834. /* For each stored lock that this unlock overlaps
  835. completely, unlock it. */
  836. int stored_rc = 0;
  837. struct cifsLockInfo *li, *tmp;
  838. rc = 0;
  839. mutex_lock(&fid->lock_mutex);
  840. list_for_each_entry_safe(li, tmp, &fid->llist, llist) {
  841. if (pfLock->fl_start <= li->offset &&
  842. (pfLock->fl_start + length) >=
  843. (li->offset + li->length)) {
  844. stored_rc = CIFSSMBLock(xid, tcon,
  845. netfid,
  846. li->length, li->offset,
  847. 1, 0, li->type, false);
  848. if (stored_rc)
  849. rc = stored_rc;
  850. list_del(&li->llist);
  851. kfree(li);
  852. }
  853. }
  854. mutex_unlock(&fid->lock_mutex);
  855. }
  856. }
  857. if (pfLock->fl_flags & FL_POSIX)
  858. posix_lock_file_wait(file, pfLock);
  859. FreeXid(xid);
  860. return rc;
  861. }
  862. /*
  863. * Set the timeout on write requests past EOF. For some servers (Windows)
  864. * these calls can be very long.
  865. *
  866. * If we're writing >10M past the EOF we give a 180s timeout. Anything less
  867. * than that gets a 45s timeout. Writes not past EOF get 15s timeouts.
  868. * The 10M cutoff is totally arbitrary. A better scheme for this would be
  869. * welcome if someone wants to suggest one.
  870. *
  871. * We may be able to do a better job with this if there were some way to
  872. * declare that a file should be sparse.
  873. */
  874. static int
  875. cifs_write_timeout(struct cifsInodeInfo *cifsi, loff_t offset)
  876. {
  877. if (offset <= cifsi->server_eof)
  878. return CIFS_STD_OP;
  879. else if (offset > (cifsi->server_eof + (10 * 1024 * 1024)))
  880. return CIFS_VLONG_OP;
  881. else
  882. return CIFS_LONG_OP;
  883. }
  884. /* update the file size (if needed) after a write */
  885. static void
  886. cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
  887. unsigned int bytes_written)
  888. {
  889. loff_t end_of_write = offset + bytes_written;
  890. if (end_of_write > cifsi->server_eof)
  891. cifsi->server_eof = end_of_write;
  892. }
  893. ssize_t cifs_user_write(struct file *file, const char __user *write_data,
  894. size_t write_size, loff_t *poffset)
  895. {
  896. int rc = 0;
  897. unsigned int bytes_written = 0;
  898. unsigned int total_written;
  899. struct cifs_sb_info *cifs_sb;
  900. struct cifsTconInfo *pTcon;
  901. int xid, long_op;
  902. struct cifsFileInfo *open_file;
  903. struct cifsInodeInfo *cifsi = CIFS_I(file->f_path.dentry->d_inode);
  904. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  905. pTcon = cifs_sb->tcon;
  906. /* cFYI(1,
  907. (" write %d bytes to offset %lld of %s", write_size,
  908. *poffset, file->f_path.dentry->d_name.name)); */
  909. if (file->private_data == NULL)
  910. return -EBADF;
  911. open_file = (struct cifsFileInfo *) file->private_data;
  912. rc = generic_write_checks(file, poffset, &write_size, 0);
  913. if (rc)
  914. return rc;
  915. xid = GetXid();
  916. long_op = cifs_write_timeout(cifsi, *poffset);
  917. for (total_written = 0; write_size > total_written;
  918. total_written += bytes_written) {
  919. rc = -EAGAIN;
  920. while (rc == -EAGAIN) {
  921. if (file->private_data == NULL) {
  922. /* file has been closed on us */
  923. FreeXid(xid);
  924. /* if we have gotten here we have written some data
  925. and blocked, and the file has been freed on us while
  926. we blocked so return what we managed to write */
  927. return total_written;
  928. }
  929. if (open_file->closePend) {
  930. FreeXid(xid);
  931. if (total_written)
  932. return total_written;
  933. else
  934. return -EBADF;
  935. }
  936. if (open_file->invalidHandle) {
  937. /* we could deadlock if we called
  938. filemap_fdatawait from here so tell
  939. reopen_file not to flush data to server
  940. now */
  941. rc = cifs_reopen_file(file, false);
  942. if (rc != 0)
  943. break;
  944. }
  945. rc = CIFSSMBWrite(xid, pTcon,
  946. open_file->netfid,
  947. min_t(const int, cifs_sb->wsize,
  948. write_size - total_written),
  949. *poffset, &bytes_written,
  950. NULL, write_data + total_written, long_op);
  951. }
  952. if (rc || (bytes_written == 0)) {
  953. if (total_written)
  954. break;
  955. else {
  956. FreeXid(xid);
  957. return rc;
  958. }
  959. } else {
  960. cifs_update_eof(cifsi, *poffset, bytes_written);
  961. *poffset += bytes_written;
  962. }
  963. long_op = CIFS_STD_OP; /* subsequent writes fast -
  964. 15 seconds is plenty */
  965. }
  966. cifs_stats_bytes_written(pTcon, total_written);
  967. /* since the write may have blocked check these pointers again */
  968. if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) {
  969. struct inode *inode = file->f_path.dentry->d_inode;
  970. /* Do not update local mtime - server will set its actual value on write
  971. * inode->i_ctime = inode->i_mtime =
  972. * current_fs_time(inode->i_sb);*/
  973. if (total_written > 0) {
  974. spin_lock(&inode->i_lock);
  975. if (*poffset > file->f_path.dentry->d_inode->i_size)
  976. i_size_write(file->f_path.dentry->d_inode,
  977. *poffset);
  978. spin_unlock(&inode->i_lock);
  979. }
  980. mark_inode_dirty_sync(file->f_path.dentry->d_inode);
  981. }
  982. FreeXid(xid);
  983. return total_written;
  984. }
  985. static ssize_t cifs_write(struct file *file, const char *write_data,
  986. size_t write_size, loff_t *poffset)
  987. {
  988. int rc = 0;
  989. unsigned int bytes_written = 0;
  990. unsigned int total_written;
  991. struct cifs_sb_info *cifs_sb;
  992. struct cifsTconInfo *pTcon;
  993. int xid, long_op;
  994. struct cifsFileInfo *open_file;
  995. struct cifsInodeInfo *cifsi = CIFS_I(file->f_path.dentry->d_inode);
  996. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  997. pTcon = cifs_sb->tcon;
  998. cFYI(1, ("write %zd bytes to offset %lld of %s", write_size,
  999. *poffset, file->f_path.dentry->d_name.name));
  1000. if (file->private_data == NULL)
  1001. return -EBADF;
  1002. open_file = (struct cifsFileInfo *)file->private_data;
  1003. xid = GetXid();
  1004. long_op = cifs_write_timeout(cifsi, *poffset);
  1005. for (total_written = 0; write_size > total_written;
  1006. total_written += bytes_written) {
  1007. rc = -EAGAIN;
  1008. while (rc == -EAGAIN) {
  1009. if (file->private_data == NULL) {
  1010. /* file has been closed on us */
  1011. FreeXid(xid);
  1012. /* if we have gotten here we have written some data
  1013. and blocked, and the file has been freed on us
  1014. while we blocked so return what we managed to
  1015. write */
  1016. return total_written;
  1017. }
  1018. if (open_file->closePend) {
  1019. FreeXid(xid);
  1020. if (total_written)
  1021. return total_written;
  1022. else
  1023. return -EBADF;
  1024. }
  1025. if (open_file->invalidHandle) {
  1026. /* we could deadlock if we called
  1027. filemap_fdatawait from here so tell
  1028. reopen_file not to flush data to
  1029. server now */
  1030. rc = cifs_reopen_file(file, false);
  1031. if (rc != 0)
  1032. break;
  1033. }
  1034. if (experimEnabled || (pTcon->ses->server &&
  1035. ((pTcon->ses->server->secMode &
  1036. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  1037. == 0))) {
  1038. struct kvec iov[2];
  1039. unsigned int len;
  1040. len = min((size_t)cifs_sb->wsize,
  1041. write_size - total_written);
  1042. /* iov[0] is reserved for smb header */
  1043. iov[1].iov_base = (char *)write_data +
  1044. total_written;
  1045. iov[1].iov_len = len;
  1046. rc = CIFSSMBWrite2(xid, pTcon,
  1047. open_file->netfid, len,
  1048. *poffset, &bytes_written,
  1049. iov, 1, long_op);
  1050. } else
  1051. rc = CIFSSMBWrite(xid, pTcon,
  1052. open_file->netfid,
  1053. min_t(const int, cifs_sb->wsize,
  1054. write_size - total_written),
  1055. *poffset, &bytes_written,
  1056. write_data + total_written,
  1057. NULL, long_op);
  1058. }
  1059. if (rc || (bytes_written == 0)) {
  1060. if (total_written)
  1061. break;
  1062. else {
  1063. FreeXid(xid);
  1064. return rc;
  1065. }
  1066. } else {
  1067. cifs_update_eof(cifsi, *poffset, bytes_written);
  1068. *poffset += bytes_written;
  1069. }
  1070. long_op = CIFS_STD_OP; /* subsequent writes fast -
  1071. 15 seconds is plenty */
  1072. }
  1073. cifs_stats_bytes_written(pTcon, total_written);
  1074. /* since the write may have blocked check these pointers again */
  1075. if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) {
  1076. /*BB We could make this contingent on superblock ATIME flag too */
  1077. /* file->f_path.dentry->d_inode->i_ctime =
  1078. file->f_path.dentry->d_inode->i_mtime = CURRENT_TIME;*/
  1079. if (total_written > 0) {
  1080. spin_lock(&file->f_path.dentry->d_inode->i_lock);
  1081. if (*poffset > file->f_path.dentry->d_inode->i_size)
  1082. i_size_write(file->f_path.dentry->d_inode,
  1083. *poffset);
  1084. spin_unlock(&file->f_path.dentry->d_inode->i_lock);
  1085. }
  1086. mark_inode_dirty_sync(file->f_path.dentry->d_inode);
  1087. }
  1088. FreeXid(xid);
  1089. return total_written;
  1090. }
  1091. #ifdef CONFIG_CIFS_EXPERIMENTAL
  1092. struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode)
  1093. {
  1094. struct cifsFileInfo *open_file = NULL;
  1095. read_lock(&GlobalSMBSeslock);
  1096. /* we could simply get the first_list_entry since write-only entries
  1097. are always at the end of the list but since the first entry might
  1098. have a close pending, we go through the whole list */
  1099. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1100. if (open_file->closePend)
  1101. continue;
  1102. if (open_file->pfile && ((open_file->pfile->f_flags & O_RDWR) ||
  1103. (open_file->pfile->f_flags & O_RDONLY))) {
  1104. if (!open_file->invalidHandle) {
  1105. /* found a good file */
  1106. /* lock it so it will not be closed on us */
  1107. atomic_inc(&open_file->wrtPending);
  1108. read_unlock(&GlobalSMBSeslock);
  1109. return open_file;
  1110. } /* else might as well continue, and look for
  1111. another, or simply have the caller reopen it
  1112. again rather than trying to fix this handle */
  1113. } else /* write only file */
  1114. break; /* write only files are last so must be done */
  1115. }
  1116. read_unlock(&GlobalSMBSeslock);
  1117. return NULL;
  1118. }
  1119. #endif
  1120. struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode)
  1121. {
  1122. struct cifsFileInfo *open_file;
  1123. bool any_available = false;
  1124. int rc;
  1125. /* Having a null inode here (because mapping->host was set to zero by
  1126. the VFS or MM) should not happen but we had reports of on oops (due to
  1127. it being zero) during stress testcases so we need to check for it */
  1128. if (cifs_inode == NULL) {
  1129. cERROR(1, ("Null inode passed to cifs_writeable_file"));
  1130. dump_stack();
  1131. return NULL;
  1132. }
  1133. read_lock(&GlobalSMBSeslock);
  1134. refind_writable:
  1135. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1136. if (open_file->closePend ||
  1137. (!any_available && open_file->pid != current->tgid))
  1138. continue;
  1139. if (open_file->pfile &&
  1140. ((open_file->pfile->f_flags & O_RDWR) ||
  1141. (open_file->pfile->f_flags & O_WRONLY))) {
  1142. atomic_inc(&open_file->wrtPending);
  1143. if (!open_file->invalidHandle) {
  1144. /* found a good writable file */
  1145. read_unlock(&GlobalSMBSeslock);
  1146. return open_file;
  1147. }
  1148. read_unlock(&GlobalSMBSeslock);
  1149. /* Had to unlock since following call can block */
  1150. rc = cifs_reopen_file(open_file->pfile, false);
  1151. if (!rc) {
  1152. if (!open_file->closePend)
  1153. return open_file;
  1154. else { /* start over in case this was deleted */
  1155. /* since the list could be modified */
  1156. read_lock(&GlobalSMBSeslock);
  1157. atomic_dec(&open_file->wrtPending);
  1158. goto refind_writable;
  1159. }
  1160. }
  1161. /* if it fails, try another handle if possible -
  1162. (we can not do this if closePending since
  1163. loop could be modified - in which case we
  1164. have to start at the beginning of the list
  1165. again. Note that it would be bad
  1166. to hold up writepages here (rather than
  1167. in caller) with continuous retries */
  1168. cFYI(1, ("wp failed on reopen file"));
  1169. read_lock(&GlobalSMBSeslock);
  1170. /* can not use this handle, no write
  1171. pending on this one after all */
  1172. atomic_dec(&open_file->wrtPending);
  1173. if (open_file->closePend) /* list could have changed */
  1174. goto refind_writable;
  1175. /* else we simply continue to the next entry. Thus
  1176. we do not loop on reopen errors. If we
  1177. can not reopen the file, for example if we
  1178. reconnected to a server with another client
  1179. racing to delete or lock the file we would not
  1180. make progress if we restarted before the beginning
  1181. of the loop here. */
  1182. }
  1183. }
  1184. /* couldn't find useable FH with same pid, try any available */
  1185. if (!any_available) {
  1186. any_available = true;
  1187. goto refind_writable;
  1188. }
  1189. read_unlock(&GlobalSMBSeslock);
  1190. return NULL;
  1191. }
  1192. static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
  1193. {
  1194. struct address_space *mapping = page->mapping;
  1195. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1196. char *write_data;
  1197. int rc = -EFAULT;
  1198. int bytes_written = 0;
  1199. struct cifs_sb_info *cifs_sb;
  1200. struct cifsTconInfo *pTcon;
  1201. struct inode *inode;
  1202. struct cifsFileInfo *open_file;
  1203. if (!mapping || !mapping->host)
  1204. return -EFAULT;
  1205. inode = page->mapping->host;
  1206. cifs_sb = CIFS_SB(inode->i_sb);
  1207. pTcon = cifs_sb->tcon;
  1208. offset += (loff_t)from;
  1209. write_data = kmap(page);
  1210. write_data += from;
  1211. if ((to > PAGE_CACHE_SIZE) || (from > to)) {
  1212. kunmap(page);
  1213. return -EIO;
  1214. }
  1215. /* racing with truncate? */
  1216. if (offset > mapping->host->i_size) {
  1217. kunmap(page);
  1218. return 0; /* don't care */
  1219. }
  1220. /* check to make sure that we are not extending the file */
  1221. if (mapping->host->i_size - offset < (loff_t)to)
  1222. to = (unsigned)(mapping->host->i_size - offset);
  1223. open_file = find_writable_file(CIFS_I(mapping->host));
  1224. if (open_file) {
  1225. bytes_written = cifs_write(open_file->pfile, write_data,
  1226. to-from, &offset);
  1227. atomic_dec(&open_file->wrtPending);
  1228. /* Does mm or vfs already set times? */
  1229. inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb);
  1230. if ((bytes_written > 0) && (offset))
  1231. rc = 0;
  1232. else if (bytes_written < 0)
  1233. rc = bytes_written;
  1234. } else {
  1235. cFYI(1, ("No writeable filehandles for inode"));
  1236. rc = -EIO;
  1237. }
  1238. kunmap(page);
  1239. return rc;
  1240. }
  1241. static int cifs_writepages(struct address_space *mapping,
  1242. struct writeback_control *wbc)
  1243. {
  1244. struct backing_dev_info *bdi = mapping->backing_dev_info;
  1245. unsigned int bytes_to_write;
  1246. unsigned int bytes_written;
  1247. struct cifs_sb_info *cifs_sb;
  1248. int done = 0;
  1249. pgoff_t end;
  1250. pgoff_t index;
  1251. int range_whole = 0;
  1252. struct kvec *iov;
  1253. int len;
  1254. int n_iov = 0;
  1255. pgoff_t next;
  1256. int nr_pages;
  1257. __u64 offset = 0;
  1258. struct cifsFileInfo *open_file;
  1259. struct cifsInodeInfo *cifsi = CIFS_I(mapping->host);
  1260. struct page *page;
  1261. struct pagevec pvec;
  1262. int rc = 0;
  1263. int scanned = 0;
  1264. int xid, long_op;
  1265. cifs_sb = CIFS_SB(mapping->host->i_sb);
  1266. /*
  1267. * If wsize is smaller that the page cache size, default to writing
  1268. * one page at a time via cifs_writepage
  1269. */
  1270. if (cifs_sb->wsize < PAGE_CACHE_SIZE)
  1271. return generic_writepages(mapping, wbc);
  1272. if ((cifs_sb->tcon->ses) && (cifs_sb->tcon->ses->server))
  1273. if (cifs_sb->tcon->ses->server->secMode &
  1274. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  1275. if (!experimEnabled)
  1276. return generic_writepages(mapping, wbc);
  1277. iov = kmalloc(32 * sizeof(struct kvec), GFP_KERNEL);
  1278. if (iov == NULL)
  1279. return generic_writepages(mapping, wbc);
  1280. /*
  1281. * BB: Is this meaningful for a non-block-device file system?
  1282. * If it is, we should test it again after we do I/O
  1283. */
  1284. if (wbc->nonblocking && bdi_write_congested(bdi)) {
  1285. wbc->encountered_congestion = 1;
  1286. kfree(iov);
  1287. return 0;
  1288. }
  1289. xid = GetXid();
  1290. pagevec_init(&pvec, 0);
  1291. if (wbc->range_cyclic) {
  1292. index = mapping->writeback_index; /* Start from prev offset */
  1293. end = -1;
  1294. } else {
  1295. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  1296. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1297. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  1298. range_whole = 1;
  1299. scanned = 1;
  1300. }
  1301. retry:
  1302. while (!done && (index <= end) &&
  1303. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  1304. PAGECACHE_TAG_DIRTY,
  1305. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1))) {
  1306. int first;
  1307. unsigned int i;
  1308. first = -1;
  1309. next = 0;
  1310. n_iov = 0;
  1311. bytes_to_write = 0;
  1312. for (i = 0; i < nr_pages; i++) {
  1313. page = pvec.pages[i];
  1314. /*
  1315. * At this point we hold neither mapping->tree_lock nor
  1316. * lock on the page itself: the page may be truncated or
  1317. * invalidated (changing page->mapping to NULL), or even
  1318. * swizzled back from swapper_space to tmpfs file
  1319. * mapping
  1320. */
  1321. if (first < 0)
  1322. lock_page(page);
  1323. else if (!trylock_page(page))
  1324. break;
  1325. if (unlikely(page->mapping != mapping)) {
  1326. unlock_page(page);
  1327. break;
  1328. }
  1329. if (!wbc->range_cyclic && page->index > end) {
  1330. done = 1;
  1331. unlock_page(page);
  1332. break;
  1333. }
  1334. if (next && (page->index != next)) {
  1335. /* Not next consecutive page */
  1336. unlock_page(page);
  1337. break;
  1338. }
  1339. if (wbc->sync_mode != WB_SYNC_NONE)
  1340. wait_on_page_writeback(page);
  1341. if (PageWriteback(page) ||
  1342. !clear_page_dirty_for_io(page)) {
  1343. unlock_page(page);
  1344. break;
  1345. }
  1346. /*
  1347. * This actually clears the dirty bit in the radix tree.
  1348. * See cifs_writepage() for more commentary.
  1349. */
  1350. set_page_writeback(page);
  1351. if (page_offset(page) >= mapping->host->i_size) {
  1352. done = 1;
  1353. unlock_page(page);
  1354. end_page_writeback(page);
  1355. break;
  1356. }
  1357. /*
  1358. * BB can we get rid of this? pages are held by pvec
  1359. */
  1360. page_cache_get(page);
  1361. len = min(mapping->host->i_size - page_offset(page),
  1362. (loff_t)PAGE_CACHE_SIZE);
  1363. /* reserve iov[0] for the smb header */
  1364. n_iov++;
  1365. iov[n_iov].iov_base = kmap(page);
  1366. iov[n_iov].iov_len = len;
  1367. bytes_to_write += len;
  1368. if (first < 0) {
  1369. first = i;
  1370. offset = page_offset(page);
  1371. }
  1372. next = page->index + 1;
  1373. if (bytes_to_write + PAGE_CACHE_SIZE > cifs_sb->wsize)
  1374. break;
  1375. }
  1376. if (n_iov) {
  1377. /* Search for a writable handle every time we call
  1378. * CIFSSMBWrite2. We can't rely on the last handle
  1379. * we used to still be valid
  1380. */
  1381. open_file = find_writable_file(CIFS_I(mapping->host));
  1382. if (!open_file) {
  1383. cERROR(1, ("No writable handles for inode"));
  1384. rc = -EBADF;
  1385. } else {
  1386. long_op = cifs_write_timeout(cifsi, offset);
  1387. rc = CIFSSMBWrite2(xid, cifs_sb->tcon,
  1388. open_file->netfid,
  1389. bytes_to_write, offset,
  1390. &bytes_written, iov, n_iov,
  1391. long_op);
  1392. atomic_dec(&open_file->wrtPending);
  1393. cifs_update_eof(cifsi, offset, bytes_written);
  1394. if (rc || bytes_written < bytes_to_write) {
  1395. cERROR(1, ("Write2 ret %d, wrote %d",
  1396. rc, bytes_written));
  1397. /* BB what if continued retry is
  1398. requested via mount flags? */
  1399. if (rc == -ENOSPC)
  1400. set_bit(AS_ENOSPC, &mapping->flags);
  1401. else
  1402. set_bit(AS_EIO, &mapping->flags);
  1403. } else {
  1404. cifs_stats_bytes_written(cifs_sb->tcon,
  1405. bytes_written);
  1406. }
  1407. }
  1408. for (i = 0; i < n_iov; i++) {
  1409. page = pvec.pages[first + i];
  1410. /* Should we also set page error on
  1411. success rc but too little data written? */
  1412. /* BB investigate retry logic on temporary
  1413. server crash cases and how recovery works
  1414. when page marked as error */
  1415. if (rc)
  1416. SetPageError(page);
  1417. kunmap(page);
  1418. unlock_page(page);
  1419. end_page_writeback(page);
  1420. page_cache_release(page);
  1421. }
  1422. if ((wbc->nr_to_write -= n_iov) <= 0)
  1423. done = 1;
  1424. index = next;
  1425. } else
  1426. /* Need to re-find the pages we skipped */
  1427. index = pvec.pages[0]->index + 1;
  1428. pagevec_release(&pvec);
  1429. }
  1430. if (!scanned && !done) {
  1431. /*
  1432. * We hit the last page and there is more work to be done: wrap
  1433. * back to the start of the file
  1434. */
  1435. scanned = 1;
  1436. index = 0;
  1437. goto retry;
  1438. }
  1439. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  1440. mapping->writeback_index = index;
  1441. FreeXid(xid);
  1442. kfree(iov);
  1443. return rc;
  1444. }
  1445. static int cifs_writepage(struct page *page, struct writeback_control *wbc)
  1446. {
  1447. int rc = -EFAULT;
  1448. int xid;
  1449. xid = GetXid();
  1450. /* BB add check for wbc flags */
  1451. page_cache_get(page);
  1452. if (!PageUptodate(page))
  1453. cFYI(1, ("ppw - page not up to date"));
  1454. /*
  1455. * Set the "writeback" flag, and clear "dirty" in the radix tree.
  1456. *
  1457. * A writepage() implementation always needs to do either this,
  1458. * or re-dirty the page with "redirty_page_for_writepage()" in
  1459. * the case of a failure.
  1460. *
  1461. * Just unlocking the page will cause the radix tree tag-bits
  1462. * to fail to update with the state of the page correctly.
  1463. */
  1464. set_page_writeback(page);
  1465. rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE);
  1466. SetPageUptodate(page); /* BB add check for error and Clearuptodate? */
  1467. unlock_page(page);
  1468. end_page_writeback(page);
  1469. page_cache_release(page);
  1470. FreeXid(xid);
  1471. return rc;
  1472. }
  1473. static int cifs_write_end(struct file *file, struct address_space *mapping,
  1474. loff_t pos, unsigned len, unsigned copied,
  1475. struct page *page, void *fsdata)
  1476. {
  1477. int rc;
  1478. struct inode *inode = mapping->host;
  1479. cFYI(1, ("write_end for page %p from pos %lld with %d bytes",
  1480. page, pos, copied));
  1481. if (PageChecked(page)) {
  1482. if (copied == len)
  1483. SetPageUptodate(page);
  1484. ClearPageChecked(page);
  1485. } else if (!PageUptodate(page) && copied == PAGE_CACHE_SIZE)
  1486. SetPageUptodate(page);
  1487. if (!PageUptodate(page)) {
  1488. char *page_data;
  1489. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  1490. int xid;
  1491. xid = GetXid();
  1492. /* this is probably better than directly calling
  1493. partialpage_write since in this function the file handle is
  1494. known which we might as well leverage */
  1495. /* BB check if anything else missing out of ppw
  1496. such as updating last write time */
  1497. page_data = kmap(page);
  1498. rc = cifs_write(file, page_data + offset, copied, &pos);
  1499. /* if (rc < 0) should we set writebehind rc? */
  1500. kunmap(page);
  1501. FreeXid(xid);
  1502. } else {
  1503. rc = copied;
  1504. pos += copied;
  1505. set_page_dirty(page);
  1506. }
  1507. if (rc > 0) {
  1508. spin_lock(&inode->i_lock);
  1509. if (pos > inode->i_size)
  1510. i_size_write(inode, pos);
  1511. spin_unlock(&inode->i_lock);
  1512. }
  1513. unlock_page(page);
  1514. page_cache_release(page);
  1515. return rc;
  1516. }
  1517. int cifs_fsync(struct file *file, struct dentry *dentry, int datasync)
  1518. {
  1519. int xid;
  1520. int rc = 0;
  1521. struct cifsTconInfo *tcon;
  1522. struct cifsFileInfo *smbfile =
  1523. (struct cifsFileInfo *)file->private_data;
  1524. struct inode *inode = file->f_path.dentry->d_inode;
  1525. xid = GetXid();
  1526. cFYI(1, ("Sync file - name: %s datasync: 0x%x",
  1527. dentry->d_name.name, datasync));
  1528. rc = filemap_write_and_wait(inode->i_mapping);
  1529. if (rc == 0) {
  1530. rc = CIFS_I(inode)->write_behind_rc;
  1531. CIFS_I(inode)->write_behind_rc = 0;
  1532. tcon = CIFS_SB(inode->i_sb)->tcon;
  1533. if (!rc && tcon && smbfile &&
  1534. !(CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
  1535. rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
  1536. }
  1537. FreeXid(xid);
  1538. return rc;
  1539. }
  1540. /* static void cifs_sync_page(struct page *page)
  1541. {
  1542. struct address_space *mapping;
  1543. struct inode *inode;
  1544. unsigned long index = page->index;
  1545. unsigned int rpages = 0;
  1546. int rc = 0;
  1547. cFYI(1, ("sync page %p",page));
  1548. mapping = page->mapping;
  1549. if (!mapping)
  1550. return 0;
  1551. inode = mapping->host;
  1552. if (!inode)
  1553. return; */
  1554. /* fill in rpages then
  1555. result = cifs_pagein_inode(inode, index, rpages); */ /* BB finish */
  1556. /* cFYI(1, ("rpages is %d for sync page of Index %ld", rpages, index));
  1557. #if 0
  1558. if (rc < 0)
  1559. return rc;
  1560. return 0;
  1561. #endif
  1562. } */
  1563. /*
  1564. * As file closes, flush all cached write data for this inode checking
  1565. * for write behind errors.
  1566. */
  1567. int cifs_flush(struct file *file, fl_owner_t id)
  1568. {
  1569. struct inode *inode = file->f_path.dentry->d_inode;
  1570. int rc = 0;
  1571. /* Rather than do the steps manually:
  1572. lock the inode for writing
  1573. loop through pages looking for write behind data (dirty pages)
  1574. coalesce into contiguous 16K (or smaller) chunks to write to server
  1575. send to server (prefer in parallel)
  1576. deal with writebehind errors
  1577. unlock inode for writing
  1578. filemapfdatawrite appears easier for the time being */
  1579. rc = filemap_fdatawrite(inode->i_mapping);
  1580. /* reset wb rc if we were able to write out dirty pages */
  1581. if (!rc) {
  1582. rc = CIFS_I(inode)->write_behind_rc;
  1583. CIFS_I(inode)->write_behind_rc = 0;
  1584. }
  1585. cFYI(1, ("Flush inode %p file %p rc %d", inode, file, rc));
  1586. return rc;
  1587. }
  1588. ssize_t cifs_user_read(struct file *file, char __user *read_data,
  1589. size_t read_size, loff_t *poffset)
  1590. {
  1591. int rc = -EACCES;
  1592. unsigned int bytes_read = 0;
  1593. unsigned int total_read = 0;
  1594. unsigned int current_read_size;
  1595. struct cifs_sb_info *cifs_sb;
  1596. struct cifsTconInfo *pTcon;
  1597. int xid;
  1598. struct cifsFileInfo *open_file;
  1599. char *smb_read_data;
  1600. char __user *current_offset;
  1601. struct smb_com_read_rsp *pSMBr;
  1602. xid = GetXid();
  1603. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1604. pTcon = cifs_sb->tcon;
  1605. if (file->private_data == NULL) {
  1606. FreeXid(xid);
  1607. return -EBADF;
  1608. }
  1609. open_file = (struct cifsFileInfo *)file->private_data;
  1610. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  1611. cFYI(1, ("attempting read on write only file instance"));
  1612. for (total_read = 0, current_offset = read_data;
  1613. read_size > total_read;
  1614. total_read += bytes_read, current_offset += bytes_read) {
  1615. current_read_size = min_t(const int, read_size - total_read,
  1616. cifs_sb->rsize);
  1617. rc = -EAGAIN;
  1618. smb_read_data = NULL;
  1619. while (rc == -EAGAIN) {
  1620. int buf_type = CIFS_NO_BUFFER;
  1621. if ((open_file->invalidHandle) &&
  1622. (!open_file->closePend)) {
  1623. rc = cifs_reopen_file(file, true);
  1624. if (rc != 0)
  1625. break;
  1626. }
  1627. rc = CIFSSMBRead(xid, pTcon,
  1628. open_file->netfid,
  1629. current_read_size, *poffset,
  1630. &bytes_read, &smb_read_data,
  1631. &buf_type);
  1632. pSMBr = (struct smb_com_read_rsp *)smb_read_data;
  1633. if (smb_read_data) {
  1634. if (copy_to_user(current_offset,
  1635. smb_read_data +
  1636. 4 /* RFC1001 length field */ +
  1637. le16_to_cpu(pSMBr->DataOffset),
  1638. bytes_read))
  1639. rc = -EFAULT;
  1640. if (buf_type == CIFS_SMALL_BUFFER)
  1641. cifs_small_buf_release(smb_read_data);
  1642. else if (buf_type == CIFS_LARGE_BUFFER)
  1643. cifs_buf_release(smb_read_data);
  1644. smb_read_data = NULL;
  1645. }
  1646. }
  1647. if (rc || (bytes_read == 0)) {
  1648. if (total_read) {
  1649. break;
  1650. } else {
  1651. FreeXid(xid);
  1652. return rc;
  1653. }
  1654. } else {
  1655. cifs_stats_bytes_read(pTcon, bytes_read);
  1656. *poffset += bytes_read;
  1657. }
  1658. }
  1659. FreeXid(xid);
  1660. return total_read;
  1661. }
  1662. static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
  1663. loff_t *poffset)
  1664. {
  1665. int rc = -EACCES;
  1666. unsigned int bytes_read = 0;
  1667. unsigned int total_read;
  1668. unsigned int current_read_size;
  1669. struct cifs_sb_info *cifs_sb;
  1670. struct cifsTconInfo *pTcon;
  1671. int xid;
  1672. char *current_offset;
  1673. struct cifsFileInfo *open_file;
  1674. int buf_type = CIFS_NO_BUFFER;
  1675. xid = GetXid();
  1676. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1677. pTcon = cifs_sb->tcon;
  1678. if (file->private_data == NULL) {
  1679. FreeXid(xid);
  1680. return -EBADF;
  1681. }
  1682. open_file = (struct cifsFileInfo *)file->private_data;
  1683. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  1684. cFYI(1, ("attempting read on write only file instance"));
  1685. for (total_read = 0, current_offset = read_data;
  1686. read_size > total_read;
  1687. total_read += bytes_read, current_offset += bytes_read) {
  1688. current_read_size = min_t(const int, read_size - total_read,
  1689. cifs_sb->rsize);
  1690. /* For windows me and 9x we do not want to request more
  1691. than it negotiated since it will refuse the read then */
  1692. if ((pTcon->ses) &&
  1693. !(pTcon->ses->capabilities & CAP_LARGE_FILES)) {
  1694. current_read_size = min_t(const int, current_read_size,
  1695. pTcon->ses->server->maxBuf - 128);
  1696. }
  1697. rc = -EAGAIN;
  1698. while (rc == -EAGAIN) {
  1699. if ((open_file->invalidHandle) &&
  1700. (!open_file->closePend)) {
  1701. rc = cifs_reopen_file(file, true);
  1702. if (rc != 0)
  1703. break;
  1704. }
  1705. rc = CIFSSMBRead(xid, pTcon,
  1706. open_file->netfid,
  1707. current_read_size, *poffset,
  1708. &bytes_read, &current_offset,
  1709. &buf_type);
  1710. }
  1711. if (rc || (bytes_read == 0)) {
  1712. if (total_read) {
  1713. break;
  1714. } else {
  1715. FreeXid(xid);
  1716. return rc;
  1717. }
  1718. } else {
  1719. cifs_stats_bytes_read(pTcon, total_read);
  1720. *poffset += bytes_read;
  1721. }
  1722. }
  1723. FreeXid(xid);
  1724. return total_read;
  1725. }
  1726. int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
  1727. {
  1728. struct dentry *dentry = file->f_path.dentry;
  1729. int rc, xid;
  1730. xid = GetXid();
  1731. rc = cifs_revalidate(dentry);
  1732. if (rc) {
  1733. cFYI(1, ("Validation prior to mmap failed, error=%d", rc));
  1734. FreeXid(xid);
  1735. return rc;
  1736. }
  1737. rc = generic_file_mmap(file, vma);
  1738. FreeXid(xid);
  1739. return rc;
  1740. }
  1741. static void cifs_copy_cache_pages(struct address_space *mapping,
  1742. struct list_head *pages, int bytes_read, char *data,
  1743. struct pagevec *plru_pvec)
  1744. {
  1745. struct page *page;
  1746. char *target;
  1747. while (bytes_read > 0) {
  1748. if (list_empty(pages))
  1749. break;
  1750. page = list_entry(pages->prev, struct page, lru);
  1751. list_del(&page->lru);
  1752. if (add_to_page_cache(page, mapping, page->index,
  1753. GFP_KERNEL)) {
  1754. page_cache_release(page);
  1755. cFYI(1, ("Add page cache failed"));
  1756. data += PAGE_CACHE_SIZE;
  1757. bytes_read -= PAGE_CACHE_SIZE;
  1758. continue;
  1759. }
  1760. target = kmap_atomic(page, KM_USER0);
  1761. if (PAGE_CACHE_SIZE > bytes_read) {
  1762. memcpy(target, data, bytes_read);
  1763. /* zero the tail end of this partial page */
  1764. memset(target + bytes_read, 0,
  1765. PAGE_CACHE_SIZE - bytes_read);
  1766. bytes_read = 0;
  1767. } else {
  1768. memcpy(target, data, PAGE_CACHE_SIZE);
  1769. bytes_read -= PAGE_CACHE_SIZE;
  1770. }
  1771. kunmap_atomic(target, KM_USER0);
  1772. flush_dcache_page(page);
  1773. SetPageUptodate(page);
  1774. unlock_page(page);
  1775. if (!pagevec_add(plru_pvec, page))
  1776. __pagevec_lru_add_file(plru_pvec);
  1777. data += PAGE_CACHE_SIZE;
  1778. }
  1779. return;
  1780. }
  1781. static int cifs_readpages(struct file *file, struct address_space *mapping,
  1782. struct list_head *page_list, unsigned num_pages)
  1783. {
  1784. int rc = -EACCES;
  1785. int xid;
  1786. loff_t offset;
  1787. struct page *page;
  1788. struct cifs_sb_info *cifs_sb;
  1789. struct cifsTconInfo *pTcon;
  1790. unsigned int bytes_read = 0;
  1791. unsigned int read_size, i;
  1792. char *smb_read_data = NULL;
  1793. struct smb_com_read_rsp *pSMBr;
  1794. struct pagevec lru_pvec;
  1795. struct cifsFileInfo *open_file;
  1796. int buf_type = CIFS_NO_BUFFER;
  1797. xid = GetXid();
  1798. if (file->private_data == NULL) {
  1799. FreeXid(xid);
  1800. return -EBADF;
  1801. }
  1802. open_file = (struct cifsFileInfo *)file->private_data;
  1803. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1804. pTcon = cifs_sb->tcon;
  1805. pagevec_init(&lru_pvec, 0);
  1806. cFYI(DBG2, ("rpages: num pages %d", num_pages));
  1807. for (i = 0; i < num_pages; ) {
  1808. unsigned contig_pages;
  1809. struct page *tmp_page;
  1810. unsigned long expected_index;
  1811. if (list_empty(page_list))
  1812. break;
  1813. page = list_entry(page_list->prev, struct page, lru);
  1814. offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1815. /* count adjacent pages that we will read into */
  1816. contig_pages = 0;
  1817. expected_index =
  1818. list_entry(page_list->prev, struct page, lru)->index;
  1819. list_for_each_entry_reverse(tmp_page, page_list, lru) {
  1820. if (tmp_page->index == expected_index) {
  1821. contig_pages++;
  1822. expected_index++;
  1823. } else
  1824. break;
  1825. }
  1826. if (contig_pages + i > num_pages)
  1827. contig_pages = num_pages - i;
  1828. /* for reads over a certain size could initiate async
  1829. read ahead */
  1830. read_size = contig_pages * PAGE_CACHE_SIZE;
  1831. /* Read size needs to be in multiples of one page */
  1832. read_size = min_t(const unsigned int, read_size,
  1833. cifs_sb->rsize & PAGE_CACHE_MASK);
  1834. cFYI(DBG2, ("rpages: read size 0x%x contiguous pages %d",
  1835. read_size, contig_pages));
  1836. rc = -EAGAIN;
  1837. while (rc == -EAGAIN) {
  1838. if ((open_file->invalidHandle) &&
  1839. (!open_file->closePend)) {
  1840. rc = cifs_reopen_file(file, true);
  1841. if (rc != 0)
  1842. break;
  1843. }
  1844. rc = CIFSSMBRead(xid, pTcon,
  1845. open_file->netfid,
  1846. read_size, offset,
  1847. &bytes_read, &smb_read_data,
  1848. &buf_type);
  1849. /* BB more RC checks ? */
  1850. if (rc == -EAGAIN) {
  1851. if (smb_read_data) {
  1852. if (buf_type == CIFS_SMALL_BUFFER)
  1853. cifs_small_buf_release(smb_read_data);
  1854. else if (buf_type == CIFS_LARGE_BUFFER)
  1855. cifs_buf_release(smb_read_data);
  1856. smb_read_data = NULL;
  1857. }
  1858. }
  1859. }
  1860. if ((rc < 0) || (smb_read_data == NULL)) {
  1861. cFYI(1, ("Read error in readpages: %d", rc));
  1862. break;
  1863. } else if (bytes_read > 0) {
  1864. task_io_account_read(bytes_read);
  1865. pSMBr = (struct smb_com_read_rsp *)smb_read_data;
  1866. cifs_copy_cache_pages(mapping, page_list, bytes_read,
  1867. smb_read_data + 4 /* RFC1001 hdr */ +
  1868. le16_to_cpu(pSMBr->DataOffset), &lru_pvec);
  1869. i += bytes_read >> PAGE_CACHE_SHIFT;
  1870. cifs_stats_bytes_read(pTcon, bytes_read);
  1871. if ((bytes_read & PAGE_CACHE_MASK) != bytes_read) {
  1872. i++; /* account for partial page */
  1873. /* server copy of file can have smaller size
  1874. than client */
  1875. /* BB do we need to verify this common case ?
  1876. this case is ok - if we are at server EOF
  1877. we will hit it on next read */
  1878. /* break; */
  1879. }
  1880. } else {
  1881. cFYI(1, ("No bytes read (%d) at offset %lld . "
  1882. "Cleaning remaining pages from readahead list",
  1883. bytes_read, offset));
  1884. /* BB turn off caching and do new lookup on
  1885. file size at server? */
  1886. break;
  1887. }
  1888. if (smb_read_data) {
  1889. if (buf_type == CIFS_SMALL_BUFFER)
  1890. cifs_small_buf_release(smb_read_data);
  1891. else if (buf_type == CIFS_LARGE_BUFFER)
  1892. cifs_buf_release(smb_read_data);
  1893. smb_read_data = NULL;
  1894. }
  1895. bytes_read = 0;
  1896. }
  1897. pagevec_lru_add_file(&lru_pvec);
  1898. /* need to free smb_read_data buf before exit */
  1899. if (smb_read_data) {
  1900. if (buf_type == CIFS_SMALL_BUFFER)
  1901. cifs_small_buf_release(smb_read_data);
  1902. else if (buf_type == CIFS_LARGE_BUFFER)
  1903. cifs_buf_release(smb_read_data);
  1904. smb_read_data = NULL;
  1905. }
  1906. FreeXid(xid);
  1907. return rc;
  1908. }
  1909. static int cifs_readpage_worker(struct file *file, struct page *page,
  1910. loff_t *poffset)
  1911. {
  1912. char *read_data;
  1913. int rc;
  1914. page_cache_get(page);
  1915. read_data = kmap(page);
  1916. /* for reads over a certain size could initiate async read ahead */
  1917. rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset);
  1918. if (rc < 0)
  1919. goto io_error;
  1920. else
  1921. cFYI(1, ("Bytes read %d", rc));
  1922. file->f_path.dentry->d_inode->i_atime =
  1923. current_fs_time(file->f_path.dentry->d_inode->i_sb);
  1924. if (PAGE_CACHE_SIZE > rc)
  1925. memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc);
  1926. flush_dcache_page(page);
  1927. SetPageUptodate(page);
  1928. rc = 0;
  1929. io_error:
  1930. kunmap(page);
  1931. page_cache_release(page);
  1932. return rc;
  1933. }
  1934. static int cifs_readpage(struct file *file, struct page *page)
  1935. {
  1936. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1937. int rc = -EACCES;
  1938. int xid;
  1939. xid = GetXid();
  1940. if (file->private_data == NULL) {
  1941. FreeXid(xid);
  1942. return -EBADF;
  1943. }
  1944. cFYI(1, ("readpage %p at offset %d 0x%x\n",
  1945. page, (int)offset, (int)offset));
  1946. rc = cifs_readpage_worker(file, page, &offset);
  1947. unlock_page(page);
  1948. FreeXid(xid);
  1949. return rc;
  1950. }
  1951. static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
  1952. {
  1953. struct cifsFileInfo *open_file;
  1954. read_lock(&GlobalSMBSeslock);
  1955. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1956. if (open_file->closePend)
  1957. continue;
  1958. if (open_file->pfile &&
  1959. ((open_file->pfile->f_flags & O_RDWR) ||
  1960. (open_file->pfile->f_flags & O_WRONLY))) {
  1961. read_unlock(&GlobalSMBSeslock);
  1962. return 1;
  1963. }
  1964. }
  1965. read_unlock(&GlobalSMBSeslock);
  1966. return 0;
  1967. }
  1968. /* We do not want to update the file size from server for inodes
  1969. open for write - to avoid races with writepage extending
  1970. the file - in the future we could consider allowing
  1971. refreshing the inode only on increases in the file size
  1972. but this is tricky to do without racing with writebehind
  1973. page caching in the current Linux kernel design */
  1974. bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
  1975. {
  1976. if (!cifsInode)
  1977. return true;
  1978. if (is_inode_writable(cifsInode)) {
  1979. /* This inode is open for write at least once */
  1980. struct cifs_sb_info *cifs_sb;
  1981. cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
  1982. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  1983. /* since no page cache to corrupt on directio
  1984. we can change size safely */
  1985. return true;
  1986. }
  1987. if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
  1988. return true;
  1989. return false;
  1990. } else
  1991. return true;
  1992. }
  1993. static int cifs_write_begin(struct file *file, struct address_space *mapping,
  1994. loff_t pos, unsigned len, unsigned flags,
  1995. struct page **pagep, void **fsdata)
  1996. {
  1997. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1998. loff_t offset = pos & (PAGE_CACHE_SIZE - 1);
  1999. loff_t page_start = pos & PAGE_MASK;
  2000. loff_t i_size;
  2001. struct page *page;
  2002. int rc = 0;
  2003. cFYI(1, ("write_begin from %lld len %d", (long long)pos, len));
  2004. page = grab_cache_page_write_begin(mapping, index, flags);
  2005. if (!page) {
  2006. rc = -ENOMEM;
  2007. goto out;
  2008. }
  2009. if (PageUptodate(page))
  2010. goto out;
  2011. /*
  2012. * If we write a full page it will be up to date, no need to read from
  2013. * the server. If the write is short, we'll end up doing a sync write
  2014. * instead.
  2015. */
  2016. if (len == PAGE_CACHE_SIZE)
  2017. goto out;
  2018. /*
  2019. * optimize away the read when we have an oplock, and we're not
  2020. * expecting to use any of the data we'd be reading in. That
  2021. * is, when the page lies beyond the EOF, or straddles the EOF
  2022. * and the write will cover all of the existing data.
  2023. */
  2024. if (CIFS_I(mapping->host)->clientCanCacheRead) {
  2025. i_size = i_size_read(mapping->host);
  2026. if (page_start >= i_size ||
  2027. (offset == 0 && (pos + len) >= i_size)) {
  2028. zero_user_segments(page, 0, offset,
  2029. offset + len,
  2030. PAGE_CACHE_SIZE);
  2031. /*
  2032. * PageChecked means that the parts of the page
  2033. * to which we're not writing are considered up
  2034. * to date. Once the data is copied to the
  2035. * page, it can be set uptodate.
  2036. */
  2037. SetPageChecked(page);
  2038. goto out;
  2039. }
  2040. }
  2041. if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
  2042. /*
  2043. * might as well read a page, it is fast enough. If we get
  2044. * an error, we don't need to return it. cifs_write_end will
  2045. * do a sync write instead since PG_uptodate isn't set.
  2046. */
  2047. cifs_readpage_worker(file, page, &page_start);
  2048. } else {
  2049. /* we could try using another file handle if there is one -
  2050. but how would we lock it to prevent close of that handle
  2051. racing with this read? In any case
  2052. this will be written out by write_end so is fine */
  2053. }
  2054. out:
  2055. *pagep = page;
  2056. return rc;
  2057. }
  2058. const struct address_space_operations cifs_addr_ops = {
  2059. .readpage = cifs_readpage,
  2060. .readpages = cifs_readpages,
  2061. .writepage = cifs_writepage,
  2062. .writepages = cifs_writepages,
  2063. .write_begin = cifs_write_begin,
  2064. .write_end = cifs_write_end,
  2065. .set_page_dirty = __set_page_dirty_nobuffers,
  2066. /* .sync_page = cifs_sync_page, */
  2067. /* .direct_IO = */
  2068. };
  2069. /*
  2070. * cifs_readpages requires the server to support a buffer large enough to
  2071. * contain the header plus one complete page of data. Otherwise, we need
  2072. * to leave cifs_readpages out of the address space operations.
  2073. */
  2074. const struct address_space_operations cifs_addr_ops_smallbuf = {
  2075. .readpage = cifs_readpage,
  2076. .writepage = cifs_writepage,
  2077. .writepages = cifs_writepages,
  2078. .write_begin = cifs_write_begin,
  2079. .write_end = cifs_write_end,
  2080. .set_page_dirty = __set_page_dirty_nobuffers,
  2081. /* .sync_page = cifs_sync_page, */
  2082. /* .direct_IO = */
  2083. };