file.c 60 KB

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