file.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503
  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 cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  255. struct cifsLockInfo *li, *tmp;
  256. spin_lock(&cifs_file_list_lock);
  257. if (--cifs_file->count > 0) {
  258. spin_unlock(&cifs_file_list_lock);
  259. return;
  260. }
  261. /* remove it from the lists */
  262. list_del(&cifs_file->flist);
  263. list_del(&cifs_file->tlist);
  264. if (list_empty(&cifsi->openFileList)) {
  265. cFYI(1, "closing last open instance for inode %p",
  266. cifs_file->dentry->d_inode);
  267. /* in strict cache mode we need invalidate mapping on the last
  268. close because it may cause a error when we open this file
  269. again and get at least level II oplock */
  270. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO)
  271. CIFS_I(inode)->invalid_mapping = true;
  272. cifs_set_oplock_level(cifsi, 0);
  273. }
  274. spin_unlock(&cifs_file_list_lock);
  275. if (!tcon->need_reconnect && !cifs_file->invalidHandle) {
  276. int xid, rc;
  277. xid = GetXid();
  278. rc = CIFSSMBClose(xid, tcon, cifs_file->netfid);
  279. FreeXid(xid);
  280. }
  281. /* Delete any outstanding lock records. We'll lose them when the file
  282. * is closed anyway.
  283. */
  284. mutex_lock(&cifs_file->lock_mutex);
  285. list_for_each_entry_safe(li, tmp, &cifs_file->llist, llist) {
  286. list_del(&li->llist);
  287. kfree(li);
  288. }
  289. mutex_unlock(&cifs_file->lock_mutex);
  290. cifs_put_tlink(cifs_file->tlink);
  291. dput(cifs_file->dentry);
  292. kfree(cifs_file);
  293. }
  294. int cifs_open(struct inode *inode, struct file *file)
  295. {
  296. int rc = -EACCES;
  297. int xid;
  298. __u32 oplock;
  299. struct cifs_sb_info *cifs_sb;
  300. struct cifsTconInfo *tcon;
  301. struct tcon_link *tlink;
  302. struct cifsFileInfo *pCifsFile = NULL;
  303. char *full_path = NULL;
  304. bool posix_open_ok = false;
  305. __u16 netfid;
  306. xid = GetXid();
  307. cifs_sb = CIFS_SB(inode->i_sb);
  308. tlink = cifs_sb_tlink(cifs_sb);
  309. if (IS_ERR(tlink)) {
  310. FreeXid(xid);
  311. return PTR_ERR(tlink);
  312. }
  313. tcon = tlink_tcon(tlink);
  314. full_path = build_path_from_dentry(file->f_path.dentry);
  315. if (full_path == NULL) {
  316. rc = -ENOMEM;
  317. goto out;
  318. }
  319. cFYI(1, "inode = 0x%p file flags are 0x%x for %s",
  320. inode, file->f_flags, full_path);
  321. if (oplockEnabled)
  322. oplock = REQ_OPLOCK;
  323. else
  324. oplock = 0;
  325. if (!tcon->broken_posix_open && tcon->unix_ext &&
  326. (tcon->ses->capabilities & CAP_UNIX) &&
  327. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  328. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  329. /* can not refresh inode info since size could be stale */
  330. rc = cifs_posix_open(full_path, &inode, inode->i_sb,
  331. cifs_sb->mnt_file_mode /* ignored */,
  332. file->f_flags, &oplock, &netfid, xid);
  333. if (rc == 0) {
  334. cFYI(1, "posix open succeeded");
  335. posix_open_ok = true;
  336. } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
  337. if (tcon->ses->serverNOS)
  338. cERROR(1, "server %s of type %s returned"
  339. " unexpected error on SMB posix open"
  340. ", disabling posix open support."
  341. " Check if server update available.",
  342. tcon->ses->serverName,
  343. tcon->ses->serverNOS);
  344. tcon->broken_posix_open = true;
  345. } else if ((rc != -EIO) && (rc != -EREMOTE) &&
  346. (rc != -EOPNOTSUPP)) /* path not found or net err */
  347. goto out;
  348. /* else fallthrough to retry open the old way on network i/o
  349. or DFS errors */
  350. }
  351. if (!posix_open_ok) {
  352. rc = cifs_nt_open(full_path, inode, cifs_sb, tcon,
  353. file->f_flags, &oplock, &netfid, xid);
  354. if (rc)
  355. goto out;
  356. }
  357. pCifsFile = cifs_new_fileinfo(netfid, file, tlink, oplock);
  358. if (pCifsFile == NULL) {
  359. CIFSSMBClose(xid, tcon, netfid);
  360. rc = -ENOMEM;
  361. goto out;
  362. }
  363. cifs_fscache_set_inode_cookie(inode, file);
  364. if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) {
  365. /* time to set mode which we can not set earlier due to
  366. problems creating new read-only files */
  367. struct cifs_unix_set_info_args args = {
  368. .mode = inode->i_mode,
  369. .uid = NO_CHANGE_64,
  370. .gid = NO_CHANGE_64,
  371. .ctime = NO_CHANGE_64,
  372. .atime = NO_CHANGE_64,
  373. .mtime = NO_CHANGE_64,
  374. .device = 0,
  375. };
  376. CIFSSMBUnixSetFileInfo(xid, tcon, &args, netfid,
  377. pCifsFile->pid);
  378. }
  379. out:
  380. kfree(full_path);
  381. FreeXid(xid);
  382. cifs_put_tlink(tlink);
  383. return rc;
  384. }
  385. /* Try to reacquire byte range locks that were released when session */
  386. /* to server was lost */
  387. static int cifs_relock_file(struct cifsFileInfo *cifsFile)
  388. {
  389. int rc = 0;
  390. /* BB list all locks open on this file and relock */
  391. return rc;
  392. }
  393. static int cifs_reopen_file(struct cifsFileInfo *pCifsFile, bool can_flush)
  394. {
  395. int rc = -EACCES;
  396. int xid;
  397. __u32 oplock;
  398. struct cifs_sb_info *cifs_sb;
  399. struct cifsTconInfo *tcon;
  400. struct cifsInodeInfo *pCifsInode;
  401. struct inode *inode;
  402. char *full_path = NULL;
  403. int desiredAccess;
  404. int disposition = FILE_OPEN;
  405. __u16 netfid;
  406. xid = GetXid();
  407. mutex_lock(&pCifsFile->fh_mutex);
  408. if (!pCifsFile->invalidHandle) {
  409. mutex_unlock(&pCifsFile->fh_mutex);
  410. rc = 0;
  411. FreeXid(xid);
  412. return rc;
  413. }
  414. inode = pCifsFile->dentry->d_inode;
  415. cifs_sb = CIFS_SB(inode->i_sb);
  416. tcon = tlink_tcon(pCifsFile->tlink);
  417. /* can not grab rename sem here because various ops, including
  418. those that already have the rename sem can end up causing writepage
  419. to get called and if the server was down that means we end up here,
  420. and we can never tell if the caller already has the rename_sem */
  421. full_path = build_path_from_dentry(pCifsFile->dentry);
  422. if (full_path == NULL) {
  423. rc = -ENOMEM;
  424. mutex_unlock(&pCifsFile->fh_mutex);
  425. FreeXid(xid);
  426. return rc;
  427. }
  428. cFYI(1, "inode = 0x%p file flags 0x%x for %s",
  429. inode, pCifsFile->f_flags, full_path);
  430. if (oplockEnabled)
  431. oplock = REQ_OPLOCK;
  432. else
  433. oplock = 0;
  434. if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) &&
  435. (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  436. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  437. /*
  438. * O_CREAT, O_EXCL and O_TRUNC already had their effect on the
  439. * original open. Must mask them off for a reopen.
  440. */
  441. unsigned int oflags = pCifsFile->f_flags &
  442. ~(O_CREAT | O_EXCL | O_TRUNC);
  443. rc = cifs_posix_open(full_path, NULL, inode->i_sb,
  444. cifs_sb->mnt_file_mode /* ignored */,
  445. oflags, &oplock, &netfid, xid);
  446. if (rc == 0) {
  447. cFYI(1, "posix reopen succeeded");
  448. goto reopen_success;
  449. }
  450. /* fallthrough to retry open the old way on errors, especially
  451. in the reconnect path it is important to retry hard */
  452. }
  453. desiredAccess = cifs_convert_flags(pCifsFile->f_flags);
  454. /* Can not refresh inode by passing in file_info buf to be returned
  455. by SMBOpen and then calling get_inode_info with returned buf
  456. since file might have write behind data that needs to be flushed
  457. and server version of file size can be stale. If we knew for sure
  458. that inode was not dirty locally we could do this */
  459. rc = CIFSSMBOpen(xid, tcon, full_path, disposition, desiredAccess,
  460. CREATE_NOT_DIR, &netfid, &oplock, NULL,
  461. cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
  462. CIFS_MOUNT_MAP_SPECIAL_CHR);
  463. if (rc) {
  464. mutex_unlock(&pCifsFile->fh_mutex);
  465. cFYI(1, "cifs_open returned 0x%x", rc);
  466. cFYI(1, "oplock: %d", oplock);
  467. goto reopen_error_exit;
  468. }
  469. reopen_success:
  470. pCifsFile->netfid = netfid;
  471. pCifsFile->invalidHandle = false;
  472. mutex_unlock(&pCifsFile->fh_mutex);
  473. pCifsInode = CIFS_I(inode);
  474. if (can_flush) {
  475. rc = filemap_write_and_wait(inode->i_mapping);
  476. mapping_set_error(inode->i_mapping, rc);
  477. if (tcon->unix_ext)
  478. rc = cifs_get_inode_info_unix(&inode,
  479. full_path, inode->i_sb, xid);
  480. else
  481. rc = cifs_get_inode_info(&inode,
  482. full_path, NULL, inode->i_sb,
  483. xid, NULL);
  484. } /* else we are writing out data to server already
  485. and could deadlock if we tried to flush data, and
  486. since we do not know if we have data that would
  487. invalidate the current end of file on the server
  488. we can not go to the server to get the new inod
  489. info */
  490. cifs_set_oplock_level(pCifsInode, oplock);
  491. cifs_relock_file(pCifsFile);
  492. reopen_error_exit:
  493. kfree(full_path);
  494. FreeXid(xid);
  495. return rc;
  496. }
  497. int cifs_close(struct inode *inode, struct file *file)
  498. {
  499. cifsFileInfo_put(file->private_data);
  500. file->private_data = NULL;
  501. /* return code from the ->release op is always ignored */
  502. return 0;
  503. }
  504. int cifs_closedir(struct inode *inode, struct file *file)
  505. {
  506. int rc = 0;
  507. int xid;
  508. struct cifsFileInfo *pCFileStruct = file->private_data;
  509. char *ptmp;
  510. cFYI(1, "Closedir inode = 0x%p", inode);
  511. xid = GetXid();
  512. if (pCFileStruct) {
  513. struct cifsTconInfo *pTcon = tlink_tcon(pCFileStruct->tlink);
  514. cFYI(1, "Freeing private data in close dir");
  515. spin_lock(&cifs_file_list_lock);
  516. if (!pCFileStruct->srch_inf.endOfSearch &&
  517. !pCFileStruct->invalidHandle) {
  518. pCFileStruct->invalidHandle = true;
  519. spin_unlock(&cifs_file_list_lock);
  520. rc = CIFSFindClose(xid, pTcon, pCFileStruct->netfid);
  521. cFYI(1, "Closing uncompleted readdir with rc %d",
  522. rc);
  523. /* not much we can do if it fails anyway, ignore rc */
  524. rc = 0;
  525. } else
  526. spin_unlock(&cifs_file_list_lock);
  527. ptmp = pCFileStruct->srch_inf.ntwrk_buf_start;
  528. if (ptmp) {
  529. cFYI(1, "closedir free smb buf in srch struct");
  530. pCFileStruct->srch_inf.ntwrk_buf_start = NULL;
  531. if (pCFileStruct->srch_inf.smallBuf)
  532. cifs_small_buf_release(ptmp);
  533. else
  534. cifs_buf_release(ptmp);
  535. }
  536. cifs_put_tlink(pCFileStruct->tlink);
  537. kfree(file->private_data);
  538. file->private_data = NULL;
  539. }
  540. /* BB can we lock the filestruct while this is going on? */
  541. FreeXid(xid);
  542. return rc;
  543. }
  544. static int store_file_lock(struct cifsFileInfo *fid, __u64 len,
  545. __u64 offset, __u8 lockType)
  546. {
  547. struct cifsLockInfo *li =
  548. kmalloc(sizeof(struct cifsLockInfo), GFP_KERNEL);
  549. if (li == NULL)
  550. return -ENOMEM;
  551. li->offset = offset;
  552. li->length = len;
  553. li->type = lockType;
  554. mutex_lock(&fid->lock_mutex);
  555. list_add(&li->llist, &fid->llist);
  556. mutex_unlock(&fid->lock_mutex);
  557. return 0;
  558. }
  559. int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock)
  560. {
  561. int rc, xid;
  562. __u32 numLock = 0;
  563. __u32 numUnlock = 0;
  564. __u64 length;
  565. bool wait_flag = false;
  566. struct cifs_sb_info *cifs_sb;
  567. struct cifsTconInfo *tcon;
  568. __u16 netfid;
  569. __u8 lockType = LOCKING_ANDX_LARGE_FILES;
  570. bool posix_locking = 0;
  571. length = 1 + pfLock->fl_end - pfLock->fl_start;
  572. rc = -EACCES;
  573. xid = GetXid();
  574. cFYI(1, "Lock parm: 0x%x flockflags: "
  575. "0x%x flocktype: 0x%x start: %lld end: %lld",
  576. cmd, pfLock->fl_flags, pfLock->fl_type, pfLock->fl_start,
  577. pfLock->fl_end);
  578. if (pfLock->fl_flags & FL_POSIX)
  579. cFYI(1, "Posix");
  580. if (pfLock->fl_flags & FL_FLOCK)
  581. cFYI(1, "Flock");
  582. if (pfLock->fl_flags & FL_SLEEP) {
  583. cFYI(1, "Blocking lock");
  584. wait_flag = true;
  585. }
  586. if (pfLock->fl_flags & FL_ACCESS)
  587. cFYI(1, "Process suspended by mandatory locking - "
  588. "not implemented yet");
  589. if (pfLock->fl_flags & FL_LEASE)
  590. cFYI(1, "Lease on file - not implemented yet");
  591. if (pfLock->fl_flags &
  592. (~(FL_POSIX | FL_FLOCK | FL_SLEEP | FL_ACCESS | FL_LEASE)))
  593. cFYI(1, "Unknown lock flags 0x%x", pfLock->fl_flags);
  594. if (pfLock->fl_type == F_WRLCK) {
  595. cFYI(1, "F_WRLCK ");
  596. numLock = 1;
  597. } else if (pfLock->fl_type == F_UNLCK) {
  598. cFYI(1, "F_UNLCK");
  599. numUnlock = 1;
  600. /* Check if unlock includes more than
  601. one lock range */
  602. } else if (pfLock->fl_type == F_RDLCK) {
  603. cFYI(1, "F_RDLCK");
  604. lockType |= LOCKING_ANDX_SHARED_LOCK;
  605. numLock = 1;
  606. } else if (pfLock->fl_type == F_EXLCK) {
  607. cFYI(1, "F_EXLCK");
  608. numLock = 1;
  609. } else if (pfLock->fl_type == F_SHLCK) {
  610. cFYI(1, "F_SHLCK");
  611. lockType |= LOCKING_ANDX_SHARED_LOCK;
  612. numLock = 1;
  613. } else
  614. cFYI(1, "Unknown type of lock");
  615. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  616. tcon = tlink_tcon(((struct cifsFileInfo *)file->private_data)->tlink);
  617. netfid = ((struct cifsFileInfo *)file->private_data)->netfid;
  618. if ((tcon->ses->capabilities & CAP_UNIX) &&
  619. (CIFS_UNIX_FCNTL_CAP & le64_to_cpu(tcon->fsUnixInfo.Capability)) &&
  620. ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOPOSIXBRL) == 0))
  621. posix_locking = 1;
  622. /* BB add code here to normalize offset and length to
  623. account for negative length which we can not accept over the
  624. wire */
  625. if (IS_GETLK(cmd)) {
  626. if (posix_locking) {
  627. int posix_lock_type;
  628. if (lockType & LOCKING_ANDX_SHARED_LOCK)
  629. posix_lock_type = CIFS_RDLCK;
  630. else
  631. posix_lock_type = CIFS_WRLCK;
  632. rc = CIFSSMBPosixLock(xid, tcon, netfid, 1 /* get */,
  633. length, pfLock,
  634. posix_lock_type, wait_flag);
  635. FreeXid(xid);
  636. return rc;
  637. }
  638. /* BB we could chain these into one lock request BB */
  639. rc = CIFSSMBLock(xid, tcon, netfid, length, pfLock->fl_start,
  640. 0, 1, lockType, 0 /* wait flag */, 0);
  641. if (rc == 0) {
  642. rc = CIFSSMBLock(xid, tcon, netfid, length,
  643. pfLock->fl_start, 1 /* numUnlock */ ,
  644. 0 /* numLock */ , lockType,
  645. 0 /* wait flag */, 0);
  646. pfLock->fl_type = F_UNLCK;
  647. if (rc != 0)
  648. cERROR(1, "Error unlocking previously locked "
  649. "range %d during test of lock", rc);
  650. rc = 0;
  651. } else {
  652. /* if rc == ERR_SHARING_VIOLATION ? */
  653. rc = 0;
  654. if (lockType & LOCKING_ANDX_SHARED_LOCK) {
  655. pfLock->fl_type = F_WRLCK;
  656. } else {
  657. rc = CIFSSMBLock(xid, tcon, netfid, length,
  658. pfLock->fl_start, 0, 1,
  659. lockType | LOCKING_ANDX_SHARED_LOCK,
  660. 0 /* wait flag */, 0);
  661. if (rc == 0) {
  662. rc = CIFSSMBLock(xid, tcon, netfid,
  663. length, pfLock->fl_start, 1, 0,
  664. lockType |
  665. LOCKING_ANDX_SHARED_LOCK,
  666. 0 /* wait flag */, 0);
  667. pfLock->fl_type = F_RDLCK;
  668. if (rc != 0)
  669. cERROR(1, "Error unlocking "
  670. "previously locked range %d "
  671. "during test of lock", rc);
  672. rc = 0;
  673. } else {
  674. pfLock->fl_type = F_WRLCK;
  675. rc = 0;
  676. }
  677. }
  678. }
  679. FreeXid(xid);
  680. return rc;
  681. }
  682. if (!numLock && !numUnlock) {
  683. /* if no lock or unlock then nothing
  684. to do since we do not know what it is */
  685. FreeXid(xid);
  686. return -EOPNOTSUPP;
  687. }
  688. if (posix_locking) {
  689. int posix_lock_type;
  690. if (lockType & LOCKING_ANDX_SHARED_LOCK)
  691. posix_lock_type = CIFS_RDLCK;
  692. else
  693. posix_lock_type = CIFS_WRLCK;
  694. if (numUnlock == 1)
  695. posix_lock_type = CIFS_UNLCK;
  696. rc = CIFSSMBPosixLock(xid, tcon, netfid, 0 /* set */,
  697. length, pfLock,
  698. posix_lock_type, wait_flag);
  699. } else {
  700. struct cifsFileInfo *fid = file->private_data;
  701. if (numLock) {
  702. rc = CIFSSMBLock(xid, tcon, netfid, length,
  703. pfLock->fl_start, 0, numLock, lockType,
  704. wait_flag, 0);
  705. if (rc == 0) {
  706. /* For Windows locks we must store them. */
  707. rc = store_file_lock(fid, length,
  708. pfLock->fl_start, lockType);
  709. }
  710. } else if (numUnlock) {
  711. /* For each stored lock that this unlock overlaps
  712. completely, unlock it. */
  713. int stored_rc = 0;
  714. struct cifsLockInfo *li, *tmp;
  715. rc = 0;
  716. mutex_lock(&fid->lock_mutex);
  717. list_for_each_entry_safe(li, tmp, &fid->llist, llist) {
  718. if (pfLock->fl_start <= li->offset &&
  719. (pfLock->fl_start + length) >=
  720. (li->offset + li->length)) {
  721. stored_rc = CIFSSMBLock(xid, tcon,
  722. netfid, li->length,
  723. li->offset, 1, 0,
  724. li->type, false, 0);
  725. if (stored_rc)
  726. rc = stored_rc;
  727. else {
  728. list_del(&li->llist);
  729. kfree(li);
  730. }
  731. }
  732. }
  733. mutex_unlock(&fid->lock_mutex);
  734. }
  735. }
  736. if (pfLock->fl_flags & FL_POSIX)
  737. posix_lock_file_wait(file, pfLock);
  738. FreeXid(xid);
  739. return rc;
  740. }
  741. /* update the file size (if needed) after a write */
  742. void
  743. cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset,
  744. unsigned int bytes_written)
  745. {
  746. loff_t end_of_write = offset + bytes_written;
  747. if (end_of_write > cifsi->server_eof)
  748. cifsi->server_eof = end_of_write;
  749. }
  750. ssize_t cifs_user_write(struct file *file, const char __user *write_data,
  751. size_t write_size, loff_t *poffset)
  752. {
  753. struct inode *inode = file->f_path.dentry->d_inode;
  754. int rc = 0;
  755. unsigned int bytes_written = 0;
  756. unsigned int total_written;
  757. struct cifs_sb_info *cifs_sb;
  758. struct cifsTconInfo *pTcon;
  759. int xid;
  760. struct cifsFileInfo *open_file;
  761. struct cifsInodeInfo *cifsi = CIFS_I(inode);
  762. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  763. /* cFYI(1, " write %d bytes to offset %lld of %s", write_size,
  764. *poffset, file->f_path.dentry->d_name.name); */
  765. if (file->private_data == NULL)
  766. return -EBADF;
  767. open_file = file->private_data;
  768. pTcon = tlink_tcon(open_file->tlink);
  769. rc = generic_write_checks(file, poffset, &write_size, 0);
  770. if (rc)
  771. return rc;
  772. xid = GetXid();
  773. for (total_written = 0; write_size > total_written;
  774. total_written += bytes_written) {
  775. rc = -EAGAIN;
  776. while (rc == -EAGAIN) {
  777. if (file->private_data == NULL) {
  778. /* file has been closed on us */
  779. FreeXid(xid);
  780. /* if we have gotten here we have written some data
  781. and blocked, and the file has been freed on us while
  782. we blocked so return what we managed to write */
  783. return total_written;
  784. }
  785. if (open_file->invalidHandle) {
  786. /* we could deadlock if we called
  787. filemap_fdatawait from here so tell
  788. reopen_file not to flush data to server
  789. now */
  790. rc = cifs_reopen_file(open_file, false);
  791. if (rc != 0)
  792. break;
  793. }
  794. rc = CIFSSMBWrite(xid, pTcon,
  795. open_file->netfid,
  796. min_t(const int, cifs_sb->wsize,
  797. write_size - total_written),
  798. *poffset, &bytes_written,
  799. NULL, write_data + total_written, 0);
  800. }
  801. if (rc || (bytes_written == 0)) {
  802. if (total_written)
  803. break;
  804. else {
  805. FreeXid(xid);
  806. return rc;
  807. }
  808. } else {
  809. cifs_update_eof(cifsi, *poffset, bytes_written);
  810. *poffset += bytes_written;
  811. }
  812. }
  813. cifs_stats_bytes_written(pTcon, total_written);
  814. /* Do not update local mtime - server will set its actual value on write
  815. * inode->i_ctime = inode->i_mtime =
  816. * current_fs_time(inode->i_sb);*/
  817. if (total_written > 0) {
  818. spin_lock(&inode->i_lock);
  819. if (*poffset > inode->i_size)
  820. i_size_write(inode, *poffset);
  821. spin_unlock(&inode->i_lock);
  822. }
  823. mark_inode_dirty_sync(inode);
  824. FreeXid(xid);
  825. return total_written;
  826. }
  827. static ssize_t cifs_write(struct cifsFileInfo *open_file,
  828. const char *write_data, size_t write_size,
  829. loff_t *poffset)
  830. {
  831. int rc = 0;
  832. unsigned int bytes_written = 0;
  833. unsigned int total_written;
  834. struct cifs_sb_info *cifs_sb;
  835. struct cifsTconInfo *pTcon;
  836. int xid;
  837. struct dentry *dentry = open_file->dentry;
  838. struct cifsInodeInfo *cifsi = CIFS_I(dentry->d_inode);
  839. cifs_sb = CIFS_SB(dentry->d_sb);
  840. cFYI(1, "write %zd bytes to offset %lld of %s", write_size,
  841. *poffset, dentry->d_name.name);
  842. pTcon = tlink_tcon(open_file->tlink);
  843. xid = GetXid();
  844. for (total_written = 0; write_size > total_written;
  845. total_written += bytes_written) {
  846. rc = -EAGAIN;
  847. while (rc == -EAGAIN) {
  848. if (open_file->invalidHandle) {
  849. /* we could deadlock if we called
  850. filemap_fdatawait from here so tell
  851. reopen_file not to flush data to
  852. server now */
  853. rc = cifs_reopen_file(open_file, false);
  854. if (rc != 0)
  855. break;
  856. }
  857. if (experimEnabled || (pTcon->ses->server &&
  858. ((pTcon->ses->server->secMode &
  859. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED))
  860. == 0))) {
  861. struct kvec iov[2];
  862. unsigned int len;
  863. len = min((size_t)cifs_sb->wsize,
  864. write_size - total_written);
  865. /* iov[0] is reserved for smb header */
  866. iov[1].iov_base = (char *)write_data +
  867. total_written;
  868. iov[1].iov_len = len;
  869. rc = CIFSSMBWrite2(xid, pTcon,
  870. open_file->netfid, len,
  871. *poffset, &bytes_written,
  872. iov, 1, 0);
  873. } else
  874. rc = CIFSSMBWrite(xid, pTcon,
  875. open_file->netfid,
  876. min_t(const int, cifs_sb->wsize,
  877. write_size - total_written),
  878. *poffset, &bytes_written,
  879. write_data + total_written,
  880. NULL, 0);
  881. }
  882. if (rc || (bytes_written == 0)) {
  883. if (total_written)
  884. break;
  885. else {
  886. FreeXid(xid);
  887. return rc;
  888. }
  889. } else {
  890. cifs_update_eof(cifsi, *poffset, bytes_written);
  891. *poffset += bytes_written;
  892. }
  893. }
  894. cifs_stats_bytes_written(pTcon, total_written);
  895. if (total_written > 0) {
  896. spin_lock(&dentry->d_inode->i_lock);
  897. if (*poffset > dentry->d_inode->i_size)
  898. i_size_write(dentry->d_inode, *poffset);
  899. spin_unlock(&dentry->d_inode->i_lock);
  900. }
  901. mark_inode_dirty_sync(dentry->d_inode);
  902. FreeXid(xid);
  903. return total_written;
  904. }
  905. struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode,
  906. bool fsuid_only)
  907. {
  908. struct cifsFileInfo *open_file = NULL;
  909. struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
  910. /* only filter by fsuid on multiuser mounts */
  911. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  912. fsuid_only = false;
  913. spin_lock(&cifs_file_list_lock);
  914. /* we could simply get the first_list_entry since write-only entries
  915. are always at the end of the list but since the first entry might
  916. have a close pending, we go through the whole list */
  917. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  918. if (fsuid_only && open_file->uid != current_fsuid())
  919. continue;
  920. if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) {
  921. if (!open_file->invalidHandle) {
  922. /* found a good file */
  923. /* lock it so it will not be closed on us */
  924. cifsFileInfo_get(open_file);
  925. spin_unlock(&cifs_file_list_lock);
  926. return open_file;
  927. } /* else might as well continue, and look for
  928. another, or simply have the caller reopen it
  929. again rather than trying to fix this handle */
  930. } else /* write only file */
  931. break; /* write only files are last so must be done */
  932. }
  933. spin_unlock(&cifs_file_list_lock);
  934. return NULL;
  935. }
  936. struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode,
  937. bool fsuid_only)
  938. {
  939. struct cifsFileInfo *open_file;
  940. struct cifs_sb_info *cifs_sb;
  941. bool any_available = false;
  942. int rc;
  943. /* Having a null inode here (because mapping->host was set to zero by
  944. the VFS or MM) should not happen but we had reports of on oops (due to
  945. it being zero) during stress testcases so we need to check for it */
  946. if (cifs_inode == NULL) {
  947. cERROR(1, "Null inode passed to cifs_writeable_file");
  948. dump_stack();
  949. return NULL;
  950. }
  951. cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb);
  952. /* only filter by fsuid on multiuser mounts */
  953. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
  954. fsuid_only = false;
  955. spin_lock(&cifs_file_list_lock);
  956. refind_writable:
  957. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  958. if (!any_available && open_file->pid != current->tgid)
  959. continue;
  960. if (fsuid_only && open_file->uid != current_fsuid())
  961. continue;
  962. if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
  963. cifsFileInfo_get(open_file);
  964. if (!open_file->invalidHandle) {
  965. /* found a good writable file */
  966. spin_unlock(&cifs_file_list_lock);
  967. return open_file;
  968. }
  969. spin_unlock(&cifs_file_list_lock);
  970. /* Had to unlock since following call can block */
  971. rc = cifs_reopen_file(open_file, false);
  972. if (!rc)
  973. return open_file;
  974. /* if it fails, try another handle if possible */
  975. cFYI(1, "wp failed on reopen file");
  976. cifsFileInfo_put(open_file);
  977. spin_lock(&cifs_file_list_lock);
  978. /* else we simply continue to the next entry. Thus
  979. we do not loop on reopen errors. If we
  980. can not reopen the file, for example if we
  981. reconnected to a server with another client
  982. racing to delete or lock the file we would not
  983. make progress if we restarted before the beginning
  984. of the loop here. */
  985. }
  986. }
  987. /* couldn't find useable FH with same pid, try any available */
  988. if (!any_available) {
  989. any_available = true;
  990. goto refind_writable;
  991. }
  992. spin_unlock(&cifs_file_list_lock);
  993. return NULL;
  994. }
  995. static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
  996. {
  997. struct address_space *mapping = page->mapping;
  998. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  999. char *write_data;
  1000. int rc = -EFAULT;
  1001. int bytes_written = 0;
  1002. struct inode *inode;
  1003. struct cifsFileInfo *open_file;
  1004. if (!mapping || !mapping->host)
  1005. return -EFAULT;
  1006. inode = page->mapping->host;
  1007. offset += (loff_t)from;
  1008. write_data = kmap(page);
  1009. write_data += from;
  1010. if ((to > PAGE_CACHE_SIZE) || (from > to)) {
  1011. kunmap(page);
  1012. return -EIO;
  1013. }
  1014. /* racing with truncate? */
  1015. if (offset > mapping->host->i_size) {
  1016. kunmap(page);
  1017. return 0; /* don't care */
  1018. }
  1019. /* check to make sure that we are not extending the file */
  1020. if (mapping->host->i_size - offset < (loff_t)to)
  1021. to = (unsigned)(mapping->host->i_size - offset);
  1022. open_file = find_writable_file(CIFS_I(mapping->host), false);
  1023. if (open_file) {
  1024. bytes_written = cifs_write(open_file, write_data,
  1025. to - from, &offset);
  1026. cifsFileInfo_put(open_file);
  1027. /* Does mm or vfs already set times? */
  1028. inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb);
  1029. if ((bytes_written > 0) && (offset))
  1030. rc = 0;
  1031. else if (bytes_written < 0)
  1032. rc = bytes_written;
  1033. } else {
  1034. cFYI(1, "No writeable filehandles for inode");
  1035. rc = -EIO;
  1036. }
  1037. kunmap(page);
  1038. return rc;
  1039. }
  1040. static int cifs_writepages(struct address_space *mapping,
  1041. struct writeback_control *wbc)
  1042. {
  1043. unsigned int bytes_to_write;
  1044. unsigned int bytes_written;
  1045. struct cifs_sb_info *cifs_sb;
  1046. int done = 0;
  1047. pgoff_t end;
  1048. pgoff_t index;
  1049. int range_whole = 0;
  1050. struct kvec *iov;
  1051. int len;
  1052. int n_iov = 0;
  1053. pgoff_t next;
  1054. int nr_pages;
  1055. __u64 offset = 0;
  1056. struct cifsFileInfo *open_file;
  1057. struct cifsTconInfo *tcon;
  1058. struct cifsInodeInfo *cifsi = CIFS_I(mapping->host);
  1059. struct page *page;
  1060. struct pagevec pvec;
  1061. int rc = 0;
  1062. int scanned = 0;
  1063. int xid;
  1064. cifs_sb = CIFS_SB(mapping->host->i_sb);
  1065. /*
  1066. * If wsize is smaller that the page cache size, default to writing
  1067. * one page at a time via cifs_writepage
  1068. */
  1069. if (cifs_sb->wsize < PAGE_CACHE_SIZE)
  1070. return generic_writepages(mapping, wbc);
  1071. iov = kmalloc(32 * sizeof(struct kvec), GFP_KERNEL);
  1072. if (iov == NULL)
  1073. return generic_writepages(mapping, wbc);
  1074. /*
  1075. * if there's no open file, then this is likely to fail too,
  1076. * but it'll at least handle the return. Maybe it should be
  1077. * a BUG() instead?
  1078. */
  1079. open_file = find_writable_file(CIFS_I(mapping->host), false);
  1080. if (!open_file) {
  1081. kfree(iov);
  1082. return generic_writepages(mapping, wbc);
  1083. }
  1084. tcon = tlink_tcon(open_file->tlink);
  1085. if (!experimEnabled && tcon->ses->server->secMode &
  1086. (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) {
  1087. cifsFileInfo_put(open_file);
  1088. kfree(iov);
  1089. return generic_writepages(mapping, wbc);
  1090. }
  1091. cifsFileInfo_put(open_file);
  1092. xid = GetXid();
  1093. pagevec_init(&pvec, 0);
  1094. if (wbc->range_cyclic) {
  1095. index = mapping->writeback_index; /* Start from prev offset */
  1096. end = -1;
  1097. } else {
  1098. index = wbc->range_start >> PAGE_CACHE_SHIFT;
  1099. end = wbc->range_end >> PAGE_CACHE_SHIFT;
  1100. if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
  1101. range_whole = 1;
  1102. scanned = 1;
  1103. }
  1104. retry:
  1105. while (!done && (index <= end) &&
  1106. (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
  1107. PAGECACHE_TAG_DIRTY,
  1108. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1))) {
  1109. int first;
  1110. unsigned int i;
  1111. first = -1;
  1112. next = 0;
  1113. n_iov = 0;
  1114. bytes_to_write = 0;
  1115. for (i = 0; i < nr_pages; i++) {
  1116. page = pvec.pages[i];
  1117. /*
  1118. * At this point we hold neither mapping->tree_lock nor
  1119. * lock on the page itself: the page may be truncated or
  1120. * invalidated (changing page->mapping to NULL), or even
  1121. * swizzled back from swapper_space to tmpfs file
  1122. * mapping
  1123. */
  1124. if (first < 0)
  1125. lock_page(page);
  1126. else if (!trylock_page(page))
  1127. break;
  1128. if (unlikely(page->mapping != mapping)) {
  1129. unlock_page(page);
  1130. break;
  1131. }
  1132. if (!wbc->range_cyclic && page->index > end) {
  1133. done = 1;
  1134. unlock_page(page);
  1135. break;
  1136. }
  1137. if (next && (page->index != next)) {
  1138. /* Not next consecutive page */
  1139. unlock_page(page);
  1140. break;
  1141. }
  1142. if (wbc->sync_mode != WB_SYNC_NONE)
  1143. wait_on_page_writeback(page);
  1144. if (PageWriteback(page) ||
  1145. !clear_page_dirty_for_io(page)) {
  1146. unlock_page(page);
  1147. break;
  1148. }
  1149. /*
  1150. * This actually clears the dirty bit in the radix tree.
  1151. * See cifs_writepage() for more commentary.
  1152. */
  1153. set_page_writeback(page);
  1154. if (page_offset(page) >= mapping->host->i_size) {
  1155. done = 1;
  1156. unlock_page(page);
  1157. end_page_writeback(page);
  1158. break;
  1159. }
  1160. /*
  1161. * BB can we get rid of this? pages are held by pvec
  1162. */
  1163. page_cache_get(page);
  1164. len = min(mapping->host->i_size - page_offset(page),
  1165. (loff_t)PAGE_CACHE_SIZE);
  1166. /* reserve iov[0] for the smb header */
  1167. n_iov++;
  1168. iov[n_iov].iov_base = kmap(page);
  1169. iov[n_iov].iov_len = len;
  1170. bytes_to_write += len;
  1171. if (first < 0) {
  1172. first = i;
  1173. offset = page_offset(page);
  1174. }
  1175. next = page->index + 1;
  1176. if (bytes_to_write + PAGE_CACHE_SIZE > cifs_sb->wsize)
  1177. break;
  1178. }
  1179. if (n_iov) {
  1180. retry_write:
  1181. open_file = find_writable_file(CIFS_I(mapping->host),
  1182. false);
  1183. if (!open_file) {
  1184. cERROR(1, "No writable handles for inode");
  1185. rc = -EBADF;
  1186. } else {
  1187. rc = CIFSSMBWrite2(xid, tcon, open_file->netfid,
  1188. bytes_to_write, offset,
  1189. &bytes_written, iov, n_iov,
  1190. 0);
  1191. cifsFileInfo_put(open_file);
  1192. }
  1193. cFYI(1, "Write2 rc=%d, wrote=%u", rc, bytes_written);
  1194. /*
  1195. * For now, treat a short write as if nothing got
  1196. * written. A zero length write however indicates
  1197. * ENOSPC or EFBIG. We have no way to know which
  1198. * though, so call it ENOSPC for now. EFBIG would
  1199. * get translated to AS_EIO anyway.
  1200. *
  1201. * FIXME: make it take into account the data that did
  1202. * get written
  1203. */
  1204. if (rc == 0) {
  1205. if (bytes_written == 0)
  1206. rc = -ENOSPC;
  1207. else if (bytes_written < bytes_to_write)
  1208. rc = -EAGAIN;
  1209. }
  1210. /* retry on data-integrity flush */
  1211. if (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN)
  1212. goto retry_write;
  1213. /* fix the stats and EOF */
  1214. if (bytes_written > 0) {
  1215. cifs_stats_bytes_written(tcon, bytes_written);
  1216. cifs_update_eof(cifsi, offset, bytes_written);
  1217. }
  1218. for (i = 0; i < n_iov; i++) {
  1219. page = pvec.pages[first + i];
  1220. /* on retryable write error, redirty page */
  1221. if (rc == -EAGAIN)
  1222. redirty_page_for_writepage(wbc, page);
  1223. else if (rc != 0)
  1224. SetPageError(page);
  1225. kunmap(page);
  1226. unlock_page(page);
  1227. end_page_writeback(page);
  1228. page_cache_release(page);
  1229. }
  1230. if (rc != -EAGAIN)
  1231. mapping_set_error(mapping, rc);
  1232. else
  1233. rc = 0;
  1234. if ((wbc->nr_to_write -= n_iov) <= 0)
  1235. done = 1;
  1236. index = next;
  1237. } else
  1238. /* Need to re-find the pages we skipped */
  1239. index = pvec.pages[0]->index + 1;
  1240. pagevec_release(&pvec);
  1241. }
  1242. if (!scanned && !done) {
  1243. /*
  1244. * We hit the last page and there is more work to be done: wrap
  1245. * back to the start of the file
  1246. */
  1247. scanned = 1;
  1248. index = 0;
  1249. goto retry;
  1250. }
  1251. if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
  1252. mapping->writeback_index = index;
  1253. FreeXid(xid);
  1254. kfree(iov);
  1255. return rc;
  1256. }
  1257. static int cifs_writepage(struct page *page, struct writeback_control *wbc)
  1258. {
  1259. int rc = -EFAULT;
  1260. int xid;
  1261. xid = GetXid();
  1262. /* BB add check for wbc flags */
  1263. page_cache_get(page);
  1264. if (!PageUptodate(page))
  1265. cFYI(1, "ppw - page not up to date");
  1266. /*
  1267. * Set the "writeback" flag, and clear "dirty" in the radix tree.
  1268. *
  1269. * A writepage() implementation always needs to do either this,
  1270. * or re-dirty the page with "redirty_page_for_writepage()" in
  1271. * the case of a failure.
  1272. *
  1273. * Just unlocking the page will cause the radix tree tag-bits
  1274. * to fail to update with the state of the page correctly.
  1275. */
  1276. set_page_writeback(page);
  1277. rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE);
  1278. SetPageUptodate(page); /* BB add check for error and Clearuptodate? */
  1279. unlock_page(page);
  1280. end_page_writeback(page);
  1281. page_cache_release(page);
  1282. FreeXid(xid);
  1283. return rc;
  1284. }
  1285. static int cifs_write_end(struct file *file, struct address_space *mapping,
  1286. loff_t pos, unsigned len, unsigned copied,
  1287. struct page *page, void *fsdata)
  1288. {
  1289. int rc;
  1290. struct inode *inode = mapping->host;
  1291. cFYI(1, "write_end for page %p from pos %lld with %d bytes",
  1292. page, pos, copied);
  1293. if (PageChecked(page)) {
  1294. if (copied == len)
  1295. SetPageUptodate(page);
  1296. ClearPageChecked(page);
  1297. } else if (!PageUptodate(page) && copied == PAGE_CACHE_SIZE)
  1298. SetPageUptodate(page);
  1299. if (!PageUptodate(page)) {
  1300. char *page_data;
  1301. unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
  1302. int xid;
  1303. xid = GetXid();
  1304. /* this is probably better than directly calling
  1305. partialpage_write since in this function the file handle is
  1306. known which we might as well leverage */
  1307. /* BB check if anything else missing out of ppw
  1308. such as updating last write time */
  1309. page_data = kmap(page);
  1310. rc = cifs_write(file->private_data, page_data + offset,
  1311. copied, &pos);
  1312. /* if (rc < 0) should we set writebehind rc? */
  1313. kunmap(page);
  1314. FreeXid(xid);
  1315. } else {
  1316. rc = copied;
  1317. pos += copied;
  1318. set_page_dirty(page);
  1319. }
  1320. if (rc > 0) {
  1321. spin_lock(&inode->i_lock);
  1322. if (pos > inode->i_size)
  1323. i_size_write(inode, pos);
  1324. spin_unlock(&inode->i_lock);
  1325. }
  1326. unlock_page(page);
  1327. page_cache_release(page);
  1328. return rc;
  1329. }
  1330. int cifs_strict_fsync(struct file *file, int datasync)
  1331. {
  1332. int xid;
  1333. int rc = 0;
  1334. struct cifsTconInfo *tcon;
  1335. struct cifsFileInfo *smbfile = file->private_data;
  1336. struct inode *inode = file->f_path.dentry->d_inode;
  1337. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1338. xid = GetXid();
  1339. cFYI(1, "Sync file - name: %s datasync: 0x%x",
  1340. file->f_path.dentry->d_name.name, datasync);
  1341. if (!CIFS_I(inode)->clientCanCacheRead)
  1342. cifs_invalidate_mapping(inode);
  1343. tcon = tlink_tcon(smbfile->tlink);
  1344. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
  1345. rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
  1346. FreeXid(xid);
  1347. return rc;
  1348. }
  1349. int cifs_fsync(struct file *file, int datasync)
  1350. {
  1351. int xid;
  1352. int rc = 0;
  1353. struct cifsTconInfo *tcon;
  1354. struct cifsFileInfo *smbfile = file->private_data;
  1355. struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1356. xid = GetXid();
  1357. cFYI(1, "Sync file - name: %s datasync: 0x%x",
  1358. file->f_path.dentry->d_name.name, datasync);
  1359. tcon = tlink_tcon(smbfile->tlink);
  1360. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC))
  1361. rc = CIFSSMBFlush(xid, tcon, smbfile->netfid);
  1362. FreeXid(xid);
  1363. return rc;
  1364. }
  1365. /*
  1366. * As file closes, flush all cached write data for this inode checking
  1367. * for write behind errors.
  1368. */
  1369. int cifs_flush(struct file *file, fl_owner_t id)
  1370. {
  1371. struct inode *inode = file->f_path.dentry->d_inode;
  1372. int rc = 0;
  1373. if (file->f_mode & FMODE_WRITE)
  1374. rc = filemap_write_and_wait(inode->i_mapping);
  1375. cFYI(1, "Flush inode %p file %p rc %d", inode, file, rc);
  1376. return rc;
  1377. }
  1378. static int
  1379. cifs_write_allocate_pages(struct page **pages, unsigned long num_pages)
  1380. {
  1381. int rc = 0;
  1382. unsigned long i;
  1383. for (i = 0; i < num_pages; i++) {
  1384. pages[i] = alloc_page(__GFP_HIGHMEM);
  1385. if (!pages[i]) {
  1386. /*
  1387. * save number of pages we have already allocated and
  1388. * return with ENOMEM error
  1389. */
  1390. num_pages = i;
  1391. rc = -ENOMEM;
  1392. goto error;
  1393. }
  1394. }
  1395. return rc;
  1396. error:
  1397. for (i = 0; i < num_pages; i++)
  1398. put_page(pages[i]);
  1399. return rc;
  1400. }
  1401. static inline
  1402. size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len)
  1403. {
  1404. size_t num_pages;
  1405. size_t clen;
  1406. clen = min_t(const size_t, len, wsize);
  1407. num_pages = clen / PAGE_CACHE_SIZE;
  1408. if (clen % PAGE_CACHE_SIZE)
  1409. num_pages++;
  1410. if (cur_len)
  1411. *cur_len = clen;
  1412. return num_pages;
  1413. }
  1414. static ssize_t
  1415. cifs_iovec_write(struct file *file, const struct iovec *iov,
  1416. unsigned long nr_segs, loff_t *poffset)
  1417. {
  1418. unsigned int written;
  1419. unsigned long num_pages, npages, i;
  1420. size_t copied, len, cur_len;
  1421. ssize_t total_written = 0;
  1422. struct kvec *to_send;
  1423. struct page **pages;
  1424. struct iov_iter it;
  1425. struct inode *inode;
  1426. struct cifsFileInfo *open_file;
  1427. struct cifsTconInfo *pTcon;
  1428. struct cifs_sb_info *cifs_sb;
  1429. int xid, rc;
  1430. len = iov_length(iov, nr_segs);
  1431. if (!len)
  1432. return 0;
  1433. rc = generic_write_checks(file, poffset, &len, 0);
  1434. if (rc)
  1435. return rc;
  1436. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1437. num_pages = get_numpages(cifs_sb->wsize, len, &cur_len);
  1438. pages = kmalloc(sizeof(struct pages *)*num_pages, GFP_KERNEL);
  1439. if (!pages)
  1440. return -ENOMEM;
  1441. to_send = kmalloc(sizeof(struct kvec)*(num_pages + 1), GFP_KERNEL);
  1442. if (!to_send) {
  1443. kfree(pages);
  1444. return -ENOMEM;
  1445. }
  1446. rc = cifs_write_allocate_pages(pages, num_pages);
  1447. if (rc) {
  1448. kfree(pages);
  1449. kfree(to_send);
  1450. return rc;
  1451. }
  1452. xid = GetXid();
  1453. open_file = file->private_data;
  1454. pTcon = tlink_tcon(open_file->tlink);
  1455. inode = file->f_path.dentry->d_inode;
  1456. iov_iter_init(&it, iov, nr_segs, len, 0);
  1457. npages = num_pages;
  1458. do {
  1459. size_t save_len = cur_len;
  1460. for (i = 0; i < npages; i++) {
  1461. copied = min_t(const size_t, cur_len, PAGE_CACHE_SIZE);
  1462. copied = iov_iter_copy_from_user(pages[i], &it, 0,
  1463. copied);
  1464. cur_len -= copied;
  1465. iov_iter_advance(&it, copied);
  1466. to_send[i+1].iov_base = kmap(pages[i]);
  1467. to_send[i+1].iov_len = copied;
  1468. }
  1469. cur_len = save_len - cur_len;
  1470. do {
  1471. if (open_file->invalidHandle) {
  1472. rc = cifs_reopen_file(open_file, false);
  1473. if (rc != 0)
  1474. break;
  1475. }
  1476. rc = CIFSSMBWrite2(xid, pTcon, open_file->netfid,
  1477. cur_len, *poffset, &written,
  1478. to_send, npages, 0);
  1479. } while (rc == -EAGAIN);
  1480. for (i = 0; i < npages; i++)
  1481. kunmap(pages[i]);
  1482. if (written) {
  1483. len -= written;
  1484. total_written += written;
  1485. cifs_update_eof(CIFS_I(inode), *poffset, written);
  1486. *poffset += written;
  1487. } else if (rc < 0) {
  1488. if (!total_written)
  1489. total_written = rc;
  1490. break;
  1491. }
  1492. /* get length and number of kvecs of the next write */
  1493. npages = get_numpages(cifs_sb->wsize, len, &cur_len);
  1494. } while (len > 0);
  1495. if (total_written > 0) {
  1496. spin_lock(&inode->i_lock);
  1497. if (*poffset > inode->i_size)
  1498. i_size_write(inode, *poffset);
  1499. spin_unlock(&inode->i_lock);
  1500. }
  1501. cifs_stats_bytes_written(pTcon, total_written);
  1502. mark_inode_dirty_sync(inode);
  1503. for (i = 0; i < num_pages; i++)
  1504. put_page(pages[i]);
  1505. kfree(to_send);
  1506. kfree(pages);
  1507. FreeXid(xid);
  1508. return total_written;
  1509. }
  1510. static ssize_t cifs_user_writev(struct kiocb *iocb, const struct iovec *iov,
  1511. unsigned long nr_segs, loff_t pos)
  1512. {
  1513. ssize_t written;
  1514. struct inode *inode;
  1515. inode = iocb->ki_filp->f_path.dentry->d_inode;
  1516. /*
  1517. * BB - optimize the way when signing is disabled. We can drop this
  1518. * extra memory-to-memory copying and use iovec buffers for constructing
  1519. * write request.
  1520. */
  1521. written = cifs_iovec_write(iocb->ki_filp, iov, nr_segs, &pos);
  1522. if (written > 0) {
  1523. CIFS_I(inode)->invalid_mapping = true;
  1524. iocb->ki_pos = pos;
  1525. }
  1526. return written;
  1527. }
  1528. ssize_t cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov,
  1529. unsigned long nr_segs, loff_t pos)
  1530. {
  1531. struct inode *inode;
  1532. inode = iocb->ki_filp->f_path.dentry->d_inode;
  1533. if (CIFS_I(inode)->clientCanCacheAll)
  1534. return generic_file_aio_write(iocb, iov, nr_segs, pos);
  1535. /*
  1536. * In strict cache mode we need to write the data to the server exactly
  1537. * from the pos to pos+len-1 rather than flush all affected pages
  1538. * because it may cause a error with mandatory locks on these pages but
  1539. * not on the region from pos to ppos+len-1.
  1540. */
  1541. return cifs_user_writev(iocb, iov, nr_segs, pos);
  1542. }
  1543. static ssize_t
  1544. cifs_iovec_read(struct file *file, const struct iovec *iov,
  1545. unsigned long nr_segs, loff_t *poffset)
  1546. {
  1547. int rc;
  1548. int xid;
  1549. ssize_t total_read;
  1550. unsigned int bytes_read = 0;
  1551. size_t len, cur_len;
  1552. int iov_offset = 0;
  1553. struct cifs_sb_info *cifs_sb;
  1554. struct cifsTconInfo *pTcon;
  1555. struct cifsFileInfo *open_file;
  1556. struct smb_com_read_rsp *pSMBr;
  1557. char *read_data;
  1558. if (!nr_segs)
  1559. return 0;
  1560. len = iov_length(iov, nr_segs);
  1561. if (!len)
  1562. return 0;
  1563. xid = GetXid();
  1564. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1565. open_file = file->private_data;
  1566. pTcon = tlink_tcon(open_file->tlink);
  1567. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  1568. cFYI(1, "attempting read on write only file instance");
  1569. for (total_read = 0; total_read < len; total_read += bytes_read) {
  1570. cur_len = min_t(const size_t, len - total_read, cifs_sb->rsize);
  1571. rc = -EAGAIN;
  1572. read_data = NULL;
  1573. while (rc == -EAGAIN) {
  1574. int buf_type = CIFS_NO_BUFFER;
  1575. if (open_file->invalidHandle) {
  1576. rc = cifs_reopen_file(open_file, true);
  1577. if (rc != 0)
  1578. break;
  1579. }
  1580. rc = CIFSSMBRead(xid, pTcon, open_file->netfid,
  1581. cur_len, *poffset, &bytes_read,
  1582. &read_data, &buf_type);
  1583. pSMBr = (struct smb_com_read_rsp *)read_data;
  1584. if (read_data) {
  1585. char *data_offset = read_data + 4 +
  1586. le16_to_cpu(pSMBr->DataOffset);
  1587. if (memcpy_toiovecend(iov, data_offset,
  1588. iov_offset, bytes_read))
  1589. rc = -EFAULT;
  1590. if (buf_type == CIFS_SMALL_BUFFER)
  1591. cifs_small_buf_release(read_data);
  1592. else if (buf_type == CIFS_LARGE_BUFFER)
  1593. cifs_buf_release(read_data);
  1594. read_data = NULL;
  1595. iov_offset += bytes_read;
  1596. }
  1597. }
  1598. if (rc || (bytes_read == 0)) {
  1599. if (total_read) {
  1600. break;
  1601. } else {
  1602. FreeXid(xid);
  1603. return rc;
  1604. }
  1605. } else {
  1606. cifs_stats_bytes_read(pTcon, bytes_read);
  1607. *poffset += bytes_read;
  1608. }
  1609. }
  1610. FreeXid(xid);
  1611. return total_read;
  1612. }
  1613. ssize_t cifs_user_read(struct file *file, char __user *read_data,
  1614. size_t read_size, loff_t *poffset)
  1615. {
  1616. struct iovec iov;
  1617. iov.iov_base = read_data;
  1618. iov.iov_len = read_size;
  1619. return cifs_iovec_read(file, &iov, 1, poffset);
  1620. }
  1621. static ssize_t cifs_user_readv(struct kiocb *iocb, const struct iovec *iov,
  1622. unsigned long nr_segs, loff_t pos)
  1623. {
  1624. ssize_t read;
  1625. read = cifs_iovec_read(iocb->ki_filp, iov, nr_segs, &pos);
  1626. if (read > 0)
  1627. iocb->ki_pos = pos;
  1628. return read;
  1629. }
  1630. ssize_t cifs_strict_readv(struct kiocb *iocb, const struct iovec *iov,
  1631. unsigned long nr_segs, loff_t pos)
  1632. {
  1633. struct inode *inode;
  1634. inode = iocb->ki_filp->f_path.dentry->d_inode;
  1635. if (CIFS_I(inode)->clientCanCacheRead)
  1636. return generic_file_aio_read(iocb, iov, nr_segs, pos);
  1637. /*
  1638. * In strict cache mode we need to read from the server all the time
  1639. * if we don't have level II oplock because the server can delay mtime
  1640. * change - so we can't make a decision about inode invalidating.
  1641. * And we can also fail with pagereading if there are mandatory locks
  1642. * on pages affected by this read but not on the region from pos to
  1643. * pos+len-1.
  1644. */
  1645. return cifs_user_readv(iocb, iov, nr_segs, pos);
  1646. }
  1647. static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size,
  1648. loff_t *poffset)
  1649. {
  1650. int rc = -EACCES;
  1651. unsigned int bytes_read = 0;
  1652. unsigned int total_read;
  1653. unsigned int current_read_size;
  1654. struct cifs_sb_info *cifs_sb;
  1655. struct cifsTconInfo *pTcon;
  1656. int xid;
  1657. char *current_offset;
  1658. struct cifsFileInfo *open_file;
  1659. int buf_type = CIFS_NO_BUFFER;
  1660. xid = GetXid();
  1661. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1662. if (file->private_data == NULL) {
  1663. rc = -EBADF;
  1664. FreeXid(xid);
  1665. return rc;
  1666. }
  1667. open_file = file->private_data;
  1668. pTcon = tlink_tcon(open_file->tlink);
  1669. if ((file->f_flags & O_ACCMODE) == O_WRONLY)
  1670. cFYI(1, "attempting read on write only file instance");
  1671. for (total_read = 0, current_offset = read_data;
  1672. read_size > total_read;
  1673. total_read += bytes_read, current_offset += bytes_read) {
  1674. current_read_size = min_t(const int, read_size - total_read,
  1675. cifs_sb->rsize);
  1676. /* For windows me and 9x we do not want to request more
  1677. than it negotiated since it will refuse the read then */
  1678. if ((pTcon->ses) &&
  1679. !(pTcon->ses->capabilities & CAP_LARGE_FILES)) {
  1680. current_read_size = min_t(const int, current_read_size,
  1681. pTcon->ses->server->maxBuf - 128);
  1682. }
  1683. rc = -EAGAIN;
  1684. while (rc == -EAGAIN) {
  1685. if (open_file->invalidHandle) {
  1686. rc = cifs_reopen_file(open_file, true);
  1687. if (rc != 0)
  1688. break;
  1689. }
  1690. rc = CIFSSMBRead(xid, pTcon,
  1691. open_file->netfid,
  1692. current_read_size, *poffset,
  1693. &bytes_read, &current_offset,
  1694. &buf_type);
  1695. }
  1696. if (rc || (bytes_read == 0)) {
  1697. if (total_read) {
  1698. break;
  1699. } else {
  1700. FreeXid(xid);
  1701. return rc;
  1702. }
  1703. } else {
  1704. cifs_stats_bytes_read(pTcon, total_read);
  1705. *poffset += bytes_read;
  1706. }
  1707. }
  1708. FreeXid(xid);
  1709. return total_read;
  1710. }
  1711. int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma)
  1712. {
  1713. int rc, xid;
  1714. struct inode *inode = file->f_path.dentry->d_inode;
  1715. xid = GetXid();
  1716. if (!CIFS_I(inode)->clientCanCacheRead)
  1717. cifs_invalidate_mapping(inode);
  1718. rc = generic_file_mmap(file, vma);
  1719. FreeXid(xid);
  1720. return rc;
  1721. }
  1722. int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
  1723. {
  1724. int rc, xid;
  1725. xid = GetXid();
  1726. rc = cifs_revalidate_file(file);
  1727. if (rc) {
  1728. cFYI(1, "Validation prior to mmap failed, error=%d", rc);
  1729. FreeXid(xid);
  1730. return rc;
  1731. }
  1732. rc = generic_file_mmap(file, vma);
  1733. FreeXid(xid);
  1734. return rc;
  1735. }
  1736. static void cifs_copy_cache_pages(struct address_space *mapping,
  1737. struct list_head *pages, int bytes_read, char *data)
  1738. {
  1739. struct page *page;
  1740. char *target;
  1741. while (bytes_read > 0) {
  1742. if (list_empty(pages))
  1743. break;
  1744. page = list_entry(pages->prev, struct page, lru);
  1745. list_del(&page->lru);
  1746. if (add_to_page_cache_lru(page, mapping, page->index,
  1747. GFP_KERNEL)) {
  1748. page_cache_release(page);
  1749. cFYI(1, "Add page cache failed");
  1750. data += PAGE_CACHE_SIZE;
  1751. bytes_read -= PAGE_CACHE_SIZE;
  1752. continue;
  1753. }
  1754. page_cache_release(page);
  1755. target = kmap_atomic(page, KM_USER0);
  1756. if (PAGE_CACHE_SIZE > bytes_read) {
  1757. memcpy(target, data, bytes_read);
  1758. /* zero the tail end of this partial page */
  1759. memset(target + bytes_read, 0,
  1760. PAGE_CACHE_SIZE - bytes_read);
  1761. bytes_read = 0;
  1762. } else {
  1763. memcpy(target, data, PAGE_CACHE_SIZE);
  1764. bytes_read -= PAGE_CACHE_SIZE;
  1765. }
  1766. kunmap_atomic(target, KM_USER0);
  1767. flush_dcache_page(page);
  1768. SetPageUptodate(page);
  1769. unlock_page(page);
  1770. data += PAGE_CACHE_SIZE;
  1771. /* add page to FS-Cache */
  1772. cifs_readpage_to_fscache(mapping->host, page);
  1773. }
  1774. return;
  1775. }
  1776. static int cifs_readpages(struct file *file, struct address_space *mapping,
  1777. struct list_head *page_list, unsigned num_pages)
  1778. {
  1779. int rc = -EACCES;
  1780. int xid;
  1781. loff_t offset;
  1782. struct page *page;
  1783. struct cifs_sb_info *cifs_sb;
  1784. struct cifsTconInfo *pTcon;
  1785. unsigned int bytes_read = 0;
  1786. unsigned int read_size, i;
  1787. char *smb_read_data = NULL;
  1788. struct smb_com_read_rsp *pSMBr;
  1789. struct cifsFileInfo *open_file;
  1790. int buf_type = CIFS_NO_BUFFER;
  1791. xid = GetXid();
  1792. if (file->private_data == NULL) {
  1793. rc = -EBADF;
  1794. FreeXid(xid);
  1795. return rc;
  1796. }
  1797. open_file = file->private_data;
  1798. cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
  1799. pTcon = tlink_tcon(open_file->tlink);
  1800. /*
  1801. * Reads as many pages as possible from fscache. Returns -ENOBUFS
  1802. * immediately if the cookie is negative
  1803. */
  1804. rc = cifs_readpages_from_fscache(mapping->host, mapping, page_list,
  1805. &num_pages);
  1806. if (rc == 0)
  1807. goto read_complete;
  1808. cFYI(DBG2, "rpages: num pages %d", num_pages);
  1809. for (i = 0; i < num_pages; ) {
  1810. unsigned contig_pages;
  1811. struct page *tmp_page;
  1812. unsigned long expected_index;
  1813. if (list_empty(page_list))
  1814. break;
  1815. page = list_entry(page_list->prev, struct page, lru);
  1816. offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1817. /* count adjacent pages that we will read into */
  1818. contig_pages = 0;
  1819. expected_index =
  1820. list_entry(page_list->prev, struct page, lru)->index;
  1821. list_for_each_entry_reverse(tmp_page, page_list, lru) {
  1822. if (tmp_page->index == expected_index) {
  1823. contig_pages++;
  1824. expected_index++;
  1825. } else
  1826. break;
  1827. }
  1828. if (contig_pages + i > num_pages)
  1829. contig_pages = num_pages - i;
  1830. /* for reads over a certain size could initiate async
  1831. read ahead */
  1832. read_size = contig_pages * PAGE_CACHE_SIZE;
  1833. /* Read size needs to be in multiples of one page */
  1834. read_size = min_t(const unsigned int, read_size,
  1835. cifs_sb->rsize & PAGE_CACHE_MASK);
  1836. cFYI(DBG2, "rpages: read size 0x%x contiguous pages %d",
  1837. read_size, contig_pages);
  1838. rc = -EAGAIN;
  1839. while (rc == -EAGAIN) {
  1840. if (open_file->invalidHandle) {
  1841. rc = cifs_reopen_file(open_file, true);
  1842. if (rc != 0)
  1843. break;
  1844. }
  1845. rc = CIFSSMBRead(xid, pTcon,
  1846. open_file->netfid,
  1847. read_size, offset,
  1848. &bytes_read, &smb_read_data,
  1849. &buf_type);
  1850. /* BB more RC checks ? */
  1851. if (rc == -EAGAIN) {
  1852. if (smb_read_data) {
  1853. if (buf_type == CIFS_SMALL_BUFFER)
  1854. cifs_small_buf_release(smb_read_data);
  1855. else if (buf_type == CIFS_LARGE_BUFFER)
  1856. cifs_buf_release(smb_read_data);
  1857. smb_read_data = NULL;
  1858. }
  1859. }
  1860. }
  1861. if ((rc < 0) || (smb_read_data == NULL)) {
  1862. cFYI(1, "Read error in readpages: %d", rc);
  1863. break;
  1864. } else if (bytes_read > 0) {
  1865. task_io_account_read(bytes_read);
  1866. pSMBr = (struct smb_com_read_rsp *)smb_read_data;
  1867. cifs_copy_cache_pages(mapping, page_list, bytes_read,
  1868. smb_read_data + 4 /* RFC1001 hdr */ +
  1869. le16_to_cpu(pSMBr->DataOffset));
  1870. i += bytes_read >> PAGE_CACHE_SHIFT;
  1871. cifs_stats_bytes_read(pTcon, bytes_read);
  1872. if ((bytes_read & PAGE_CACHE_MASK) != bytes_read) {
  1873. i++; /* account for partial page */
  1874. /* server copy of file can have smaller size
  1875. than client */
  1876. /* BB do we need to verify this common case ?
  1877. this case is ok - if we are at server EOF
  1878. we will hit it on next read */
  1879. /* break; */
  1880. }
  1881. } else {
  1882. cFYI(1, "No bytes read (%d) at offset %lld . "
  1883. "Cleaning remaining pages from readahead list",
  1884. bytes_read, offset);
  1885. /* BB turn off caching and do new lookup on
  1886. file size at server? */
  1887. break;
  1888. }
  1889. if (smb_read_data) {
  1890. if (buf_type == CIFS_SMALL_BUFFER)
  1891. cifs_small_buf_release(smb_read_data);
  1892. else if (buf_type == CIFS_LARGE_BUFFER)
  1893. cifs_buf_release(smb_read_data);
  1894. smb_read_data = NULL;
  1895. }
  1896. bytes_read = 0;
  1897. }
  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. read_complete:
  1907. FreeXid(xid);
  1908. return rc;
  1909. }
  1910. static int cifs_readpage_worker(struct file *file, struct page *page,
  1911. loff_t *poffset)
  1912. {
  1913. char *read_data;
  1914. int rc;
  1915. /* Is the page cached? */
  1916. rc = cifs_readpage_from_fscache(file->f_path.dentry->d_inode, page);
  1917. if (rc == 0)
  1918. goto read_complete;
  1919. page_cache_get(page);
  1920. read_data = kmap(page);
  1921. /* for reads over a certain size could initiate async read ahead */
  1922. rc = cifs_read(file, read_data, PAGE_CACHE_SIZE, poffset);
  1923. if (rc < 0)
  1924. goto io_error;
  1925. else
  1926. cFYI(1, "Bytes read %d", rc);
  1927. file->f_path.dentry->d_inode->i_atime =
  1928. current_fs_time(file->f_path.dentry->d_inode->i_sb);
  1929. if (PAGE_CACHE_SIZE > rc)
  1930. memset(read_data + rc, 0, PAGE_CACHE_SIZE - rc);
  1931. flush_dcache_page(page);
  1932. SetPageUptodate(page);
  1933. /* send this page to the cache */
  1934. cifs_readpage_to_fscache(file->f_path.dentry->d_inode, page);
  1935. rc = 0;
  1936. io_error:
  1937. kunmap(page);
  1938. page_cache_release(page);
  1939. read_complete:
  1940. return rc;
  1941. }
  1942. static int cifs_readpage(struct file *file, struct page *page)
  1943. {
  1944. loff_t offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
  1945. int rc = -EACCES;
  1946. int xid;
  1947. xid = GetXid();
  1948. if (file->private_data == NULL) {
  1949. rc = -EBADF;
  1950. FreeXid(xid);
  1951. return rc;
  1952. }
  1953. cFYI(1, "readpage %p at offset %d 0x%x\n",
  1954. page, (int)offset, (int)offset);
  1955. rc = cifs_readpage_worker(file, page, &offset);
  1956. unlock_page(page);
  1957. FreeXid(xid);
  1958. return rc;
  1959. }
  1960. static int is_inode_writable(struct cifsInodeInfo *cifs_inode)
  1961. {
  1962. struct cifsFileInfo *open_file;
  1963. spin_lock(&cifs_file_list_lock);
  1964. list_for_each_entry(open_file, &cifs_inode->openFileList, flist) {
  1965. if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) {
  1966. spin_unlock(&cifs_file_list_lock);
  1967. return 1;
  1968. }
  1969. }
  1970. spin_unlock(&cifs_file_list_lock);
  1971. return 0;
  1972. }
  1973. /* We do not want to update the file size from server for inodes
  1974. open for write - to avoid races with writepage extending
  1975. the file - in the future we could consider allowing
  1976. refreshing the inode only on increases in the file size
  1977. but this is tricky to do without racing with writebehind
  1978. page caching in the current Linux kernel design */
  1979. bool is_size_safe_to_change(struct cifsInodeInfo *cifsInode, __u64 end_of_file)
  1980. {
  1981. if (!cifsInode)
  1982. return true;
  1983. if (is_inode_writable(cifsInode)) {
  1984. /* This inode is open for write at least once */
  1985. struct cifs_sb_info *cifs_sb;
  1986. cifs_sb = CIFS_SB(cifsInode->vfs_inode.i_sb);
  1987. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  1988. /* since no page cache to corrupt on directio
  1989. we can change size safely */
  1990. return true;
  1991. }
  1992. if (i_size_read(&cifsInode->vfs_inode) < end_of_file)
  1993. return true;
  1994. return false;
  1995. } else
  1996. return true;
  1997. }
  1998. static int cifs_write_begin(struct file *file, struct address_space *mapping,
  1999. loff_t pos, unsigned len, unsigned flags,
  2000. struct page **pagep, void **fsdata)
  2001. {
  2002. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  2003. loff_t offset = pos & (PAGE_CACHE_SIZE - 1);
  2004. loff_t page_start = pos & PAGE_MASK;
  2005. loff_t i_size;
  2006. struct page *page;
  2007. int rc = 0;
  2008. cFYI(1, "write_begin from %lld len %d", (long long)pos, len);
  2009. page = grab_cache_page_write_begin(mapping, index, flags);
  2010. if (!page) {
  2011. rc = -ENOMEM;
  2012. goto out;
  2013. }
  2014. if (PageUptodate(page))
  2015. goto out;
  2016. /*
  2017. * If we write a full page it will be up to date, no need to read from
  2018. * the server. If the write is short, we'll end up doing a sync write
  2019. * instead.
  2020. */
  2021. if (len == PAGE_CACHE_SIZE)
  2022. goto out;
  2023. /*
  2024. * optimize away the read when we have an oplock, and we're not
  2025. * expecting to use any of the data we'd be reading in. That
  2026. * is, when the page lies beyond the EOF, or straddles the EOF
  2027. * and the write will cover all of the existing data.
  2028. */
  2029. if (CIFS_I(mapping->host)->clientCanCacheRead) {
  2030. i_size = i_size_read(mapping->host);
  2031. if (page_start >= i_size ||
  2032. (offset == 0 && (pos + len) >= i_size)) {
  2033. zero_user_segments(page, 0, offset,
  2034. offset + len,
  2035. PAGE_CACHE_SIZE);
  2036. /*
  2037. * PageChecked means that the parts of the page
  2038. * to which we're not writing are considered up
  2039. * to date. Once the data is copied to the
  2040. * page, it can be set uptodate.
  2041. */
  2042. SetPageChecked(page);
  2043. goto out;
  2044. }
  2045. }
  2046. if ((file->f_flags & O_ACCMODE) != O_WRONLY) {
  2047. /*
  2048. * might as well read a page, it is fast enough. If we get
  2049. * an error, we don't need to return it. cifs_write_end will
  2050. * do a sync write instead since PG_uptodate isn't set.
  2051. */
  2052. cifs_readpage_worker(file, page, &page_start);
  2053. } else {
  2054. /* we could try using another file handle if there is one -
  2055. but how would we lock it to prevent close of that handle
  2056. racing with this read? In any case
  2057. this will be written out by write_end so is fine */
  2058. }
  2059. out:
  2060. *pagep = page;
  2061. return rc;
  2062. }
  2063. static int cifs_release_page(struct page *page, gfp_t gfp)
  2064. {
  2065. if (PagePrivate(page))
  2066. return 0;
  2067. return cifs_fscache_release_page(page, gfp);
  2068. }
  2069. static void cifs_invalidate_page(struct page *page, unsigned long offset)
  2070. {
  2071. struct cifsInodeInfo *cifsi = CIFS_I(page->mapping->host);
  2072. if (offset == 0)
  2073. cifs_fscache_invalidate_page(page, &cifsi->vfs_inode);
  2074. }
  2075. void cifs_oplock_break(struct work_struct *work)
  2076. {
  2077. struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
  2078. oplock_break);
  2079. struct inode *inode = cfile->dentry->d_inode;
  2080. struct cifsInodeInfo *cinode = CIFS_I(inode);
  2081. int rc = 0;
  2082. if (inode && S_ISREG(inode->i_mode)) {
  2083. if (cinode->clientCanCacheRead)
  2084. break_lease(inode, O_RDONLY);
  2085. else
  2086. break_lease(inode, O_WRONLY);
  2087. rc = filemap_fdatawrite(inode->i_mapping);
  2088. if (cinode->clientCanCacheRead == 0) {
  2089. rc = filemap_fdatawait(inode->i_mapping);
  2090. mapping_set_error(inode->i_mapping, rc);
  2091. invalidate_remote_inode(inode);
  2092. }
  2093. cFYI(1, "Oplock flush inode %p rc %d", inode, rc);
  2094. }
  2095. /*
  2096. * releasing stale oplock after recent reconnect of smb session using
  2097. * a now incorrect file handle is not a data integrity issue but do
  2098. * not bother sending an oplock release if session to server still is
  2099. * disconnected since oplock already released by the server
  2100. */
  2101. if (!cfile->oplock_break_cancelled) {
  2102. rc = CIFSSMBLock(0, tlink_tcon(cfile->tlink), cfile->netfid, 0,
  2103. 0, 0, 0, LOCKING_ANDX_OPLOCK_RELEASE, false,
  2104. cinode->clientCanCacheRead ? 1 : 0);
  2105. cFYI(1, "Oplock release rc = %d", rc);
  2106. }
  2107. /*
  2108. * We might have kicked in before is_valid_oplock_break()
  2109. * finished grabbing reference for us. Make sure it's done by
  2110. * waiting for cifs_file_list_lock.
  2111. */
  2112. spin_lock(&cifs_file_list_lock);
  2113. spin_unlock(&cifs_file_list_lock);
  2114. cifs_oplock_break_put(cfile);
  2115. }
  2116. /* must be called while holding cifs_file_list_lock */
  2117. void cifs_oplock_break_get(struct cifsFileInfo *cfile)
  2118. {
  2119. cifs_sb_active(cfile->dentry->d_sb);
  2120. cifsFileInfo_get(cfile);
  2121. }
  2122. void cifs_oplock_break_put(struct cifsFileInfo *cfile)
  2123. {
  2124. struct super_block *sb = cfile->dentry->d_sb;
  2125. cifsFileInfo_put(cfile);
  2126. cifs_sb_deactive(sb);
  2127. }
  2128. const struct address_space_operations cifs_addr_ops = {
  2129. .readpage = cifs_readpage,
  2130. .readpages = cifs_readpages,
  2131. .writepage = cifs_writepage,
  2132. .writepages = cifs_writepages,
  2133. .write_begin = cifs_write_begin,
  2134. .write_end = cifs_write_end,
  2135. .set_page_dirty = __set_page_dirty_nobuffers,
  2136. .releasepage = cifs_release_page,
  2137. .invalidatepage = cifs_invalidate_page,
  2138. /* .direct_IO = */
  2139. };
  2140. /*
  2141. * cifs_readpages requires the server to support a buffer large enough to
  2142. * contain the header plus one complete page of data. Otherwise, we need
  2143. * to leave cifs_readpages out of the address space operations.
  2144. */
  2145. const struct address_space_operations cifs_addr_ops_smallbuf = {
  2146. .readpage = cifs_readpage,
  2147. .writepage = cifs_writepage,
  2148. .writepages = cifs_writepages,
  2149. .write_begin = cifs_write_begin,
  2150. .write_end = cifs_write_end,
  2151. .set_page_dirty = __set_page_dirty_nobuffers,
  2152. .releasepage = cifs_release_page,
  2153. .invalidatepage = cifs_invalidate_page,
  2154. /* .direct_IO = */
  2155. };