file.c 59 KB

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