file.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * file.c
  5. *
  6. * File open, close, extend, truncate
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program 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 the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. */
  25. #include <linux/capability.h>
  26. #include <linux/fs.h>
  27. #include <linux/types.h>
  28. #include <linux/slab.h>
  29. #include <linux/highmem.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/uio.h>
  32. #include <linux/sched.h>
  33. #include <linux/splice.h>
  34. #include <linux/mount.h>
  35. #include <linux/writeback.h>
  36. #include <linux/falloc.h>
  37. #include <linux/quotaops.h>
  38. #include <linux/blkdev.h>
  39. #include <cluster/masklog.h>
  40. #include "ocfs2.h"
  41. #include "alloc.h"
  42. #include "aops.h"
  43. #include "dir.h"
  44. #include "dlmglue.h"
  45. #include "extent_map.h"
  46. #include "file.h"
  47. #include "sysfile.h"
  48. #include "inode.h"
  49. #include "ioctl.h"
  50. #include "journal.h"
  51. #include "locks.h"
  52. #include "mmap.h"
  53. #include "suballoc.h"
  54. #include "super.h"
  55. #include "xattr.h"
  56. #include "acl.h"
  57. #include "quota.h"
  58. #include "refcounttree.h"
  59. #include "ocfs2_trace.h"
  60. #include "buffer_head_io.h"
  61. static int ocfs2_init_file_private(struct inode *inode, struct file *file)
  62. {
  63. struct ocfs2_file_private *fp;
  64. fp = kzalloc(sizeof(struct ocfs2_file_private), GFP_KERNEL);
  65. if (!fp)
  66. return -ENOMEM;
  67. fp->fp_file = file;
  68. mutex_init(&fp->fp_mutex);
  69. ocfs2_file_lock_res_init(&fp->fp_flock, fp);
  70. file->private_data = fp;
  71. return 0;
  72. }
  73. static void ocfs2_free_file_private(struct inode *inode, struct file *file)
  74. {
  75. struct ocfs2_file_private *fp = file->private_data;
  76. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  77. if (fp) {
  78. ocfs2_simple_drop_lockres(osb, &fp->fp_flock);
  79. ocfs2_lock_res_free(&fp->fp_flock);
  80. kfree(fp);
  81. file->private_data = NULL;
  82. }
  83. }
  84. static int ocfs2_file_open(struct inode *inode, struct file *file)
  85. {
  86. int status;
  87. int mode = file->f_flags;
  88. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  89. trace_ocfs2_file_open(inode, file, file->f_path.dentry,
  90. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  91. file->f_path.dentry->d_name.len,
  92. file->f_path.dentry->d_name.name, mode);
  93. if (file->f_mode & FMODE_WRITE)
  94. dquot_initialize(inode);
  95. spin_lock(&oi->ip_lock);
  96. /* Check that the inode hasn't been wiped from disk by another
  97. * node. If it hasn't then we're safe as long as we hold the
  98. * spin lock until our increment of open count. */
  99. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  100. spin_unlock(&oi->ip_lock);
  101. status = -ENOENT;
  102. goto leave;
  103. }
  104. if (mode & O_DIRECT)
  105. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  106. oi->ip_open_count++;
  107. spin_unlock(&oi->ip_lock);
  108. status = ocfs2_init_file_private(inode, file);
  109. if (status) {
  110. /*
  111. * We want to set open count back if we're failing the
  112. * open.
  113. */
  114. spin_lock(&oi->ip_lock);
  115. oi->ip_open_count--;
  116. spin_unlock(&oi->ip_lock);
  117. }
  118. leave:
  119. return status;
  120. }
  121. static int ocfs2_file_release(struct inode *inode, struct file *file)
  122. {
  123. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  124. spin_lock(&oi->ip_lock);
  125. if (!--oi->ip_open_count)
  126. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  127. trace_ocfs2_file_release(inode, file, file->f_path.dentry,
  128. oi->ip_blkno,
  129. file->f_path.dentry->d_name.len,
  130. file->f_path.dentry->d_name.name,
  131. oi->ip_open_count);
  132. spin_unlock(&oi->ip_lock);
  133. ocfs2_free_file_private(inode, file);
  134. return 0;
  135. }
  136. static int ocfs2_dir_open(struct inode *inode, struct file *file)
  137. {
  138. return ocfs2_init_file_private(inode, file);
  139. }
  140. static int ocfs2_dir_release(struct inode *inode, struct file *file)
  141. {
  142. ocfs2_free_file_private(inode, file);
  143. return 0;
  144. }
  145. static int ocfs2_sync_file(struct file *file, loff_t start, loff_t end,
  146. int datasync)
  147. {
  148. int err = 0;
  149. journal_t *journal;
  150. struct inode *inode = file->f_mapping->host;
  151. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  152. trace_ocfs2_sync_file(inode, file, file->f_path.dentry,
  153. OCFS2_I(inode)->ip_blkno,
  154. file->f_path.dentry->d_name.len,
  155. file->f_path.dentry->d_name.name,
  156. (unsigned long long)datasync);
  157. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  158. return -EROFS;
  159. err = filemap_write_and_wait_range(inode->i_mapping, start, end);
  160. if (err)
  161. return err;
  162. /*
  163. * Probably don't need the i_mutex at all in here, just putting it here
  164. * to be consistent with how fsync used to be called, someone more
  165. * familiar with the fs could possibly remove it.
  166. */
  167. mutex_lock(&inode->i_mutex);
  168. if (datasync && !(inode->i_state & I_DIRTY_DATASYNC)) {
  169. /*
  170. * We still have to flush drive's caches to get data to the
  171. * platter
  172. */
  173. if (osb->s_mount_opt & OCFS2_MOUNT_BARRIER)
  174. blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
  175. goto bail;
  176. }
  177. journal = osb->journal->j_journal;
  178. err = jbd2_journal_force_commit(journal);
  179. bail:
  180. if (err)
  181. mlog_errno(err);
  182. mutex_unlock(&inode->i_mutex);
  183. return (err < 0) ? -EIO : 0;
  184. }
  185. int ocfs2_should_update_atime(struct inode *inode,
  186. struct vfsmount *vfsmnt)
  187. {
  188. struct timespec now;
  189. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  190. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  191. return 0;
  192. if ((inode->i_flags & S_NOATIME) ||
  193. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  194. return 0;
  195. /*
  196. * We can be called with no vfsmnt structure - NFSD will
  197. * sometimes do this.
  198. *
  199. * Note that our action here is different than touch_atime() -
  200. * if we can't tell whether this is a noatime mount, then we
  201. * don't know whether to trust the value of s_atime_quantum.
  202. */
  203. if (vfsmnt == NULL)
  204. return 0;
  205. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  206. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  207. return 0;
  208. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  209. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  210. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  211. return 1;
  212. return 0;
  213. }
  214. now = CURRENT_TIME;
  215. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  216. return 0;
  217. else
  218. return 1;
  219. }
  220. int ocfs2_update_inode_atime(struct inode *inode,
  221. struct buffer_head *bh)
  222. {
  223. int ret;
  224. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  225. handle_t *handle;
  226. struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
  227. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  228. if (IS_ERR(handle)) {
  229. ret = PTR_ERR(handle);
  230. mlog_errno(ret);
  231. goto out;
  232. }
  233. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  234. OCFS2_JOURNAL_ACCESS_WRITE);
  235. if (ret) {
  236. mlog_errno(ret);
  237. goto out_commit;
  238. }
  239. /*
  240. * Don't use ocfs2_mark_inode_dirty() here as we don't always
  241. * have i_mutex to guard against concurrent changes to other
  242. * inode fields.
  243. */
  244. inode->i_atime = CURRENT_TIME;
  245. di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  246. di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  247. ocfs2_journal_dirty(handle, bh);
  248. out_commit:
  249. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  250. out:
  251. return ret;
  252. }
  253. static int ocfs2_set_inode_size(handle_t *handle,
  254. struct inode *inode,
  255. struct buffer_head *fe_bh,
  256. u64 new_i_size)
  257. {
  258. int status;
  259. i_size_write(inode, new_i_size);
  260. inode->i_blocks = ocfs2_inode_sector_count(inode);
  261. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  262. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  263. if (status < 0) {
  264. mlog_errno(status);
  265. goto bail;
  266. }
  267. bail:
  268. return status;
  269. }
  270. int ocfs2_simple_size_update(struct inode *inode,
  271. struct buffer_head *di_bh,
  272. u64 new_i_size)
  273. {
  274. int ret;
  275. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  276. handle_t *handle = NULL;
  277. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  278. if (IS_ERR(handle)) {
  279. ret = PTR_ERR(handle);
  280. mlog_errno(ret);
  281. goto out;
  282. }
  283. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  284. new_i_size);
  285. if (ret < 0)
  286. mlog_errno(ret);
  287. ocfs2_commit_trans(osb, handle);
  288. out:
  289. return ret;
  290. }
  291. static int ocfs2_cow_file_pos(struct inode *inode,
  292. struct buffer_head *fe_bh,
  293. u64 offset)
  294. {
  295. int status;
  296. u32 phys, cpos = offset >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  297. unsigned int num_clusters = 0;
  298. unsigned int ext_flags = 0;
  299. /*
  300. * If the new offset is aligned to the range of the cluster, there is
  301. * no space for ocfs2_zero_range_for_truncate to fill, so no need to
  302. * CoW either.
  303. */
  304. if ((offset & (OCFS2_SB(inode->i_sb)->s_clustersize - 1)) == 0)
  305. return 0;
  306. status = ocfs2_get_clusters(inode, cpos, &phys,
  307. &num_clusters, &ext_flags);
  308. if (status) {
  309. mlog_errno(status);
  310. goto out;
  311. }
  312. if (!(ext_flags & OCFS2_EXT_REFCOUNTED))
  313. goto out;
  314. return ocfs2_refcount_cow(inode, fe_bh, cpos, 1, cpos+1);
  315. out:
  316. return status;
  317. }
  318. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  319. struct inode *inode,
  320. struct buffer_head *fe_bh,
  321. u64 new_i_size)
  322. {
  323. int status;
  324. handle_t *handle;
  325. struct ocfs2_dinode *di;
  326. u64 cluster_bytes;
  327. /*
  328. * We need to CoW the cluster contains the offset if it is reflinked
  329. * since we will call ocfs2_zero_range_for_truncate later which will
  330. * write "0" from offset to the end of the cluster.
  331. */
  332. status = ocfs2_cow_file_pos(inode, fe_bh, new_i_size);
  333. if (status) {
  334. mlog_errno(status);
  335. return status;
  336. }
  337. /* TODO: This needs to actually orphan the inode in this
  338. * transaction. */
  339. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  340. if (IS_ERR(handle)) {
  341. status = PTR_ERR(handle);
  342. mlog_errno(status);
  343. goto out;
  344. }
  345. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), fe_bh,
  346. OCFS2_JOURNAL_ACCESS_WRITE);
  347. if (status < 0) {
  348. mlog_errno(status);
  349. goto out_commit;
  350. }
  351. /*
  352. * Do this before setting i_size.
  353. */
  354. cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
  355. status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
  356. cluster_bytes);
  357. if (status) {
  358. mlog_errno(status);
  359. goto out_commit;
  360. }
  361. i_size_write(inode, new_i_size);
  362. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  363. di = (struct ocfs2_dinode *) fe_bh->b_data;
  364. di->i_size = cpu_to_le64(new_i_size);
  365. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  366. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  367. ocfs2_journal_dirty(handle, fe_bh);
  368. out_commit:
  369. ocfs2_commit_trans(osb, handle);
  370. out:
  371. return status;
  372. }
  373. static int ocfs2_truncate_file(struct inode *inode,
  374. struct buffer_head *di_bh,
  375. u64 new_i_size)
  376. {
  377. int status = 0;
  378. struct ocfs2_dinode *fe = NULL;
  379. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  380. /* We trust di_bh because it comes from ocfs2_inode_lock(), which
  381. * already validated it */
  382. fe = (struct ocfs2_dinode *) di_bh->b_data;
  383. trace_ocfs2_truncate_file((unsigned long long)OCFS2_I(inode)->ip_blkno,
  384. (unsigned long long)le64_to_cpu(fe->i_size),
  385. (unsigned long long)new_i_size);
  386. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  387. "Inode %llu, inode i_size = %lld != di "
  388. "i_size = %llu, i_flags = 0x%x\n",
  389. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  390. i_size_read(inode),
  391. (unsigned long long)le64_to_cpu(fe->i_size),
  392. le32_to_cpu(fe->i_flags));
  393. if (new_i_size > le64_to_cpu(fe->i_size)) {
  394. trace_ocfs2_truncate_file_error(
  395. (unsigned long long)le64_to_cpu(fe->i_size),
  396. (unsigned long long)new_i_size);
  397. status = -EINVAL;
  398. mlog_errno(status);
  399. goto bail;
  400. }
  401. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  402. ocfs2_resv_discard(&osb->osb_la_resmap,
  403. &OCFS2_I(inode)->ip_la_data_resv);
  404. /*
  405. * The inode lock forced other nodes to sync and drop their
  406. * pages, which (correctly) happens even if we have a truncate
  407. * without allocation change - ocfs2 cluster sizes can be much
  408. * greater than page size, so we have to truncate them
  409. * anyway.
  410. */
  411. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  412. truncate_inode_pages(inode->i_mapping, new_i_size);
  413. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  414. status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
  415. i_size_read(inode), 1);
  416. if (status)
  417. mlog_errno(status);
  418. goto bail_unlock_sem;
  419. }
  420. /* alright, we're going to need to do a full blown alloc size
  421. * change. Orphan the inode so that recovery can complete the
  422. * truncate if necessary. This does the task of marking
  423. * i_size. */
  424. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  425. if (status < 0) {
  426. mlog_errno(status);
  427. goto bail_unlock_sem;
  428. }
  429. status = ocfs2_commit_truncate(osb, inode, di_bh);
  430. if (status < 0) {
  431. mlog_errno(status);
  432. goto bail_unlock_sem;
  433. }
  434. /* TODO: orphan dir cleanup here. */
  435. bail_unlock_sem:
  436. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  437. bail:
  438. if (!status && OCFS2_I(inode)->ip_clusters == 0)
  439. status = ocfs2_try_remove_refcount_tree(inode, di_bh);
  440. return status;
  441. }
  442. /*
  443. * extend file allocation only here.
  444. * we'll update all the disk stuff, and oip->alloc_size
  445. *
  446. * expect stuff to be locked, a transaction started and enough data /
  447. * metadata reservations in the contexts.
  448. *
  449. * Will return -EAGAIN, and a reason if a restart is needed.
  450. * If passed in, *reason will always be set, even in error.
  451. */
  452. int ocfs2_add_inode_data(struct ocfs2_super *osb,
  453. struct inode *inode,
  454. u32 *logical_offset,
  455. u32 clusters_to_add,
  456. int mark_unwritten,
  457. struct buffer_head *fe_bh,
  458. handle_t *handle,
  459. struct ocfs2_alloc_context *data_ac,
  460. struct ocfs2_alloc_context *meta_ac,
  461. enum ocfs2_alloc_restarted *reason_ret)
  462. {
  463. int ret;
  464. struct ocfs2_extent_tree et;
  465. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), fe_bh);
  466. ret = ocfs2_add_clusters_in_btree(handle, &et, logical_offset,
  467. clusters_to_add, mark_unwritten,
  468. data_ac, meta_ac, reason_ret);
  469. return ret;
  470. }
  471. static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  472. u32 clusters_to_add, int mark_unwritten)
  473. {
  474. int status = 0;
  475. int restart_func = 0;
  476. int credits;
  477. u32 prev_clusters;
  478. struct buffer_head *bh = NULL;
  479. struct ocfs2_dinode *fe = NULL;
  480. handle_t *handle = NULL;
  481. struct ocfs2_alloc_context *data_ac = NULL;
  482. struct ocfs2_alloc_context *meta_ac = NULL;
  483. enum ocfs2_alloc_restarted why;
  484. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  485. struct ocfs2_extent_tree et;
  486. int did_quota = 0;
  487. /*
  488. * Unwritten extent only exists for file systems which
  489. * support holes.
  490. */
  491. BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
  492. status = ocfs2_read_inode_block(inode, &bh);
  493. if (status < 0) {
  494. mlog_errno(status);
  495. goto leave;
  496. }
  497. fe = (struct ocfs2_dinode *) bh->b_data;
  498. restart_all:
  499. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  500. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), bh);
  501. status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
  502. &data_ac, &meta_ac);
  503. if (status) {
  504. mlog_errno(status);
  505. goto leave;
  506. }
  507. credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list);
  508. handle = ocfs2_start_trans(osb, credits);
  509. if (IS_ERR(handle)) {
  510. status = PTR_ERR(handle);
  511. handle = NULL;
  512. mlog_errno(status);
  513. goto leave;
  514. }
  515. restarted_transaction:
  516. trace_ocfs2_extend_allocation(
  517. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  518. (unsigned long long)i_size_read(inode),
  519. le32_to_cpu(fe->i_clusters), clusters_to_add,
  520. why, restart_func);
  521. status = dquot_alloc_space_nodirty(inode,
  522. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  523. if (status)
  524. goto leave;
  525. did_quota = 1;
  526. /* reserve a write to the file entry early on - that we if we
  527. * run out of credits in the allocation path, we can still
  528. * update i_size. */
  529. status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  530. OCFS2_JOURNAL_ACCESS_WRITE);
  531. if (status < 0) {
  532. mlog_errno(status);
  533. goto leave;
  534. }
  535. prev_clusters = OCFS2_I(inode)->ip_clusters;
  536. status = ocfs2_add_inode_data(osb,
  537. inode,
  538. &logical_start,
  539. clusters_to_add,
  540. mark_unwritten,
  541. bh,
  542. handle,
  543. data_ac,
  544. meta_ac,
  545. &why);
  546. if ((status < 0) && (status != -EAGAIN)) {
  547. if (status != -ENOSPC)
  548. mlog_errno(status);
  549. goto leave;
  550. }
  551. ocfs2_journal_dirty(handle, bh);
  552. spin_lock(&OCFS2_I(inode)->ip_lock);
  553. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  554. spin_unlock(&OCFS2_I(inode)->ip_lock);
  555. /* Release unused quota reservation */
  556. dquot_free_space(inode,
  557. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  558. did_quota = 0;
  559. if (why != RESTART_NONE && clusters_to_add) {
  560. if (why == RESTART_META) {
  561. restart_func = 1;
  562. status = 0;
  563. } else {
  564. BUG_ON(why != RESTART_TRANS);
  565. status = ocfs2_allocate_extend_trans(handle, 1);
  566. if (status < 0) {
  567. /* handle still has to be committed at
  568. * this point. */
  569. status = -ENOMEM;
  570. mlog_errno(status);
  571. goto leave;
  572. }
  573. goto restarted_transaction;
  574. }
  575. }
  576. trace_ocfs2_extend_allocation_end(OCFS2_I(inode)->ip_blkno,
  577. le32_to_cpu(fe->i_clusters),
  578. (unsigned long long)le64_to_cpu(fe->i_size),
  579. OCFS2_I(inode)->ip_clusters,
  580. (unsigned long long)i_size_read(inode));
  581. leave:
  582. if (status < 0 && did_quota)
  583. dquot_free_space(inode,
  584. ocfs2_clusters_to_bytes(osb->sb, clusters_to_add));
  585. if (handle) {
  586. ocfs2_commit_trans(osb, handle);
  587. handle = NULL;
  588. }
  589. if (data_ac) {
  590. ocfs2_free_alloc_context(data_ac);
  591. data_ac = NULL;
  592. }
  593. if (meta_ac) {
  594. ocfs2_free_alloc_context(meta_ac);
  595. meta_ac = NULL;
  596. }
  597. if ((!status) && restart_func) {
  598. restart_func = 0;
  599. goto restart_all;
  600. }
  601. brelse(bh);
  602. bh = NULL;
  603. return status;
  604. }
  605. /*
  606. * While a write will already be ordering the data, a truncate will not.
  607. * Thus, we need to explicitly order the zeroed pages.
  608. */
  609. static handle_t *ocfs2_zero_start_ordered_transaction(struct inode *inode,
  610. struct buffer_head *di_bh)
  611. {
  612. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  613. handle_t *handle = NULL;
  614. int ret = 0;
  615. if (!ocfs2_should_order_data(inode))
  616. goto out;
  617. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  618. if (IS_ERR(handle)) {
  619. ret = -ENOMEM;
  620. mlog_errno(ret);
  621. goto out;
  622. }
  623. ret = ocfs2_jbd2_file_inode(handle, inode);
  624. if (ret < 0) {
  625. mlog_errno(ret);
  626. goto out;
  627. }
  628. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  629. OCFS2_JOURNAL_ACCESS_WRITE);
  630. if (ret)
  631. mlog_errno(ret);
  632. out:
  633. if (ret) {
  634. if (!IS_ERR(handle))
  635. ocfs2_commit_trans(osb, handle);
  636. handle = ERR_PTR(ret);
  637. }
  638. return handle;
  639. }
  640. /* Some parts of this taken from generic_cont_expand, which turned out
  641. * to be too fragile to do exactly what we need without us having to
  642. * worry about recursive locking in ->write_begin() and ->write_end(). */
  643. static int ocfs2_write_zero_page(struct inode *inode, u64 abs_from,
  644. u64 abs_to, struct buffer_head *di_bh)
  645. {
  646. struct address_space *mapping = inode->i_mapping;
  647. struct page *page;
  648. unsigned long index = abs_from >> PAGE_CACHE_SHIFT;
  649. handle_t *handle = NULL;
  650. int ret = 0;
  651. unsigned zero_from, zero_to, block_start, block_end;
  652. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  653. BUG_ON(abs_from >= abs_to);
  654. BUG_ON(abs_to > (((u64)index + 1) << PAGE_CACHE_SHIFT));
  655. BUG_ON(abs_from & (inode->i_blkbits - 1));
  656. page = find_or_create_page(mapping, index, GFP_NOFS);
  657. if (!page) {
  658. ret = -ENOMEM;
  659. mlog_errno(ret);
  660. goto out;
  661. }
  662. /* Get the offsets within the page that we want to zero */
  663. zero_from = abs_from & (PAGE_CACHE_SIZE - 1);
  664. zero_to = abs_to & (PAGE_CACHE_SIZE - 1);
  665. if (!zero_to)
  666. zero_to = PAGE_CACHE_SIZE;
  667. trace_ocfs2_write_zero_page(
  668. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  669. (unsigned long long)abs_from,
  670. (unsigned long long)abs_to,
  671. index, zero_from, zero_to);
  672. /* We know that zero_from is block aligned */
  673. for (block_start = zero_from; block_start < zero_to;
  674. block_start = block_end) {
  675. block_end = block_start + (1 << inode->i_blkbits);
  676. /*
  677. * block_start is block-aligned. Bump it by one to force
  678. * __block_write_begin and block_commit_write to zero the
  679. * whole block.
  680. */
  681. ret = __block_write_begin(page, block_start + 1, 0,
  682. ocfs2_get_block);
  683. if (ret < 0) {
  684. mlog_errno(ret);
  685. goto out_unlock;
  686. }
  687. if (!handle) {
  688. handle = ocfs2_zero_start_ordered_transaction(inode,
  689. di_bh);
  690. if (IS_ERR(handle)) {
  691. ret = PTR_ERR(handle);
  692. handle = NULL;
  693. break;
  694. }
  695. }
  696. /* must not update i_size! */
  697. ret = block_commit_write(page, block_start + 1,
  698. block_start + 1);
  699. if (ret < 0)
  700. mlog_errno(ret);
  701. else
  702. ret = 0;
  703. }
  704. if (handle) {
  705. /*
  706. * fs-writeback will release the dirty pages without page lock
  707. * whose offset are over inode size, the release happens at
  708. * block_write_full_page_endio().
  709. */
  710. i_size_write(inode, abs_to);
  711. inode->i_blocks = ocfs2_inode_sector_count(inode);
  712. di->i_size = cpu_to_le64((u64)i_size_read(inode));
  713. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  714. di->i_mtime = di->i_ctime = cpu_to_le64(inode->i_mtime.tv_sec);
  715. di->i_ctime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
  716. di->i_mtime_nsec = di->i_ctime_nsec;
  717. ocfs2_journal_dirty(handle, di_bh);
  718. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  719. }
  720. out_unlock:
  721. unlock_page(page);
  722. page_cache_release(page);
  723. out:
  724. return ret;
  725. }
  726. /*
  727. * Find the next range to zero. We do this in terms of bytes because
  728. * that's what ocfs2_zero_extend() wants, and it is dealing with the
  729. * pagecache. We may return multiple extents.
  730. *
  731. * zero_start and zero_end are ocfs2_zero_extend()s current idea of what
  732. * needs to be zeroed. range_start and range_end return the next zeroing
  733. * range. A subsequent call should pass the previous range_end as its
  734. * zero_start. If range_end is 0, there's nothing to do.
  735. *
  736. * Unwritten extents are skipped over. Refcounted extents are CoWd.
  737. */
  738. static int ocfs2_zero_extend_get_range(struct inode *inode,
  739. struct buffer_head *di_bh,
  740. u64 zero_start, u64 zero_end,
  741. u64 *range_start, u64 *range_end)
  742. {
  743. int rc = 0, needs_cow = 0;
  744. u32 p_cpos, zero_clusters = 0;
  745. u32 zero_cpos =
  746. zero_start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  747. u32 last_cpos = ocfs2_clusters_for_bytes(inode->i_sb, zero_end);
  748. unsigned int num_clusters = 0;
  749. unsigned int ext_flags = 0;
  750. while (zero_cpos < last_cpos) {
  751. rc = ocfs2_get_clusters(inode, zero_cpos, &p_cpos,
  752. &num_clusters, &ext_flags);
  753. if (rc) {
  754. mlog_errno(rc);
  755. goto out;
  756. }
  757. if (p_cpos && !(ext_flags & OCFS2_EXT_UNWRITTEN)) {
  758. zero_clusters = num_clusters;
  759. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  760. needs_cow = 1;
  761. break;
  762. }
  763. zero_cpos += num_clusters;
  764. }
  765. if (!zero_clusters) {
  766. *range_end = 0;
  767. goto out;
  768. }
  769. while ((zero_cpos + zero_clusters) < last_cpos) {
  770. rc = ocfs2_get_clusters(inode, zero_cpos + zero_clusters,
  771. &p_cpos, &num_clusters,
  772. &ext_flags);
  773. if (rc) {
  774. mlog_errno(rc);
  775. goto out;
  776. }
  777. if (!p_cpos || (ext_flags & OCFS2_EXT_UNWRITTEN))
  778. break;
  779. if (ext_flags & OCFS2_EXT_REFCOUNTED)
  780. needs_cow = 1;
  781. zero_clusters += num_clusters;
  782. }
  783. if ((zero_cpos + zero_clusters) > last_cpos)
  784. zero_clusters = last_cpos - zero_cpos;
  785. if (needs_cow) {
  786. rc = ocfs2_refcount_cow(inode, di_bh, zero_cpos,
  787. zero_clusters, UINT_MAX);
  788. if (rc) {
  789. mlog_errno(rc);
  790. goto out;
  791. }
  792. }
  793. *range_start = ocfs2_clusters_to_bytes(inode->i_sb, zero_cpos);
  794. *range_end = ocfs2_clusters_to_bytes(inode->i_sb,
  795. zero_cpos + zero_clusters);
  796. out:
  797. return rc;
  798. }
  799. /*
  800. * Zero one range returned from ocfs2_zero_extend_get_range(). The caller
  801. * has made sure that the entire range needs zeroing.
  802. */
  803. static int ocfs2_zero_extend_range(struct inode *inode, u64 range_start,
  804. u64 range_end, struct buffer_head *di_bh)
  805. {
  806. int rc = 0;
  807. u64 next_pos;
  808. u64 zero_pos = range_start;
  809. trace_ocfs2_zero_extend_range(
  810. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  811. (unsigned long long)range_start,
  812. (unsigned long long)range_end);
  813. BUG_ON(range_start >= range_end);
  814. while (zero_pos < range_end) {
  815. next_pos = (zero_pos & PAGE_CACHE_MASK) + PAGE_CACHE_SIZE;
  816. if (next_pos > range_end)
  817. next_pos = range_end;
  818. rc = ocfs2_write_zero_page(inode, zero_pos, next_pos, di_bh);
  819. if (rc < 0) {
  820. mlog_errno(rc);
  821. break;
  822. }
  823. zero_pos = next_pos;
  824. /*
  825. * Very large extends have the potential to lock up
  826. * the cpu for extended periods of time.
  827. */
  828. cond_resched();
  829. }
  830. return rc;
  831. }
  832. int ocfs2_zero_extend(struct inode *inode, struct buffer_head *di_bh,
  833. loff_t zero_to_size)
  834. {
  835. int ret = 0;
  836. u64 zero_start, range_start = 0, range_end = 0;
  837. struct super_block *sb = inode->i_sb;
  838. zero_start = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  839. trace_ocfs2_zero_extend((unsigned long long)OCFS2_I(inode)->ip_blkno,
  840. (unsigned long long)zero_start,
  841. (unsigned long long)i_size_read(inode));
  842. while (zero_start < zero_to_size) {
  843. ret = ocfs2_zero_extend_get_range(inode, di_bh, zero_start,
  844. zero_to_size,
  845. &range_start,
  846. &range_end);
  847. if (ret) {
  848. mlog_errno(ret);
  849. break;
  850. }
  851. if (!range_end)
  852. break;
  853. /* Trim the ends */
  854. if (range_start < zero_start)
  855. range_start = zero_start;
  856. if (range_end > zero_to_size)
  857. range_end = zero_to_size;
  858. ret = ocfs2_zero_extend_range(inode, range_start,
  859. range_end, di_bh);
  860. if (ret) {
  861. mlog_errno(ret);
  862. break;
  863. }
  864. zero_start = range_end;
  865. }
  866. return ret;
  867. }
  868. int ocfs2_extend_no_holes(struct inode *inode, struct buffer_head *di_bh,
  869. u64 new_i_size, u64 zero_to)
  870. {
  871. int ret;
  872. u32 clusters_to_add;
  873. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  874. /*
  875. * Only quota files call this without a bh, and they can't be
  876. * refcounted.
  877. */
  878. BUG_ON(!di_bh && (oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
  879. BUG_ON(!di_bh && !(oi->ip_flags & OCFS2_INODE_SYSTEM_FILE));
  880. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
  881. if (clusters_to_add < oi->ip_clusters)
  882. clusters_to_add = 0;
  883. else
  884. clusters_to_add -= oi->ip_clusters;
  885. if (clusters_to_add) {
  886. ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
  887. clusters_to_add, 0);
  888. if (ret) {
  889. mlog_errno(ret);
  890. goto out;
  891. }
  892. }
  893. /*
  894. * Call this even if we don't add any clusters to the tree. We
  895. * still need to zero the area between the old i_size and the
  896. * new i_size.
  897. */
  898. ret = ocfs2_zero_extend(inode, di_bh, zero_to);
  899. if (ret < 0)
  900. mlog_errno(ret);
  901. out:
  902. return ret;
  903. }
  904. static int ocfs2_extend_file(struct inode *inode,
  905. struct buffer_head *di_bh,
  906. u64 new_i_size)
  907. {
  908. int ret = 0;
  909. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  910. BUG_ON(!di_bh);
  911. /* setattr sometimes calls us like this. */
  912. if (new_i_size == 0)
  913. goto out;
  914. if (i_size_read(inode) == new_i_size)
  915. goto out;
  916. BUG_ON(new_i_size < i_size_read(inode));
  917. /*
  918. * The alloc sem blocks people in read/write from reading our
  919. * allocation until we're done changing it. We depend on
  920. * i_mutex to block other extend/truncate calls while we're
  921. * here. We even have to hold it for sparse files because there
  922. * might be some tail zeroing.
  923. */
  924. down_write(&oi->ip_alloc_sem);
  925. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  926. /*
  927. * We can optimize small extends by keeping the inodes
  928. * inline data.
  929. */
  930. if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
  931. up_write(&oi->ip_alloc_sem);
  932. goto out_update_size;
  933. }
  934. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  935. if (ret) {
  936. up_write(&oi->ip_alloc_sem);
  937. mlog_errno(ret);
  938. goto out;
  939. }
  940. }
  941. if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  942. ret = ocfs2_zero_extend(inode, di_bh, new_i_size);
  943. else
  944. ret = ocfs2_extend_no_holes(inode, di_bh, new_i_size,
  945. new_i_size);
  946. up_write(&oi->ip_alloc_sem);
  947. if (ret < 0) {
  948. mlog_errno(ret);
  949. goto out;
  950. }
  951. out_update_size:
  952. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  953. if (ret < 0)
  954. mlog_errno(ret);
  955. out:
  956. return ret;
  957. }
  958. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  959. {
  960. int status = 0, size_change;
  961. struct inode *inode = dentry->d_inode;
  962. struct super_block *sb = inode->i_sb;
  963. struct ocfs2_super *osb = OCFS2_SB(sb);
  964. struct buffer_head *bh = NULL;
  965. handle_t *handle = NULL;
  966. struct dquot *transfer_to[MAXQUOTAS] = { };
  967. int qtype;
  968. trace_ocfs2_setattr(inode, dentry,
  969. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  970. dentry->d_name.len, dentry->d_name.name,
  971. attr->ia_valid, attr->ia_mode,
  972. from_kuid(&init_user_ns, attr->ia_uid),
  973. from_kgid(&init_user_ns, attr->ia_gid));
  974. /* ensuring we don't even attempt to truncate a symlink */
  975. if (S_ISLNK(inode->i_mode))
  976. attr->ia_valid &= ~ATTR_SIZE;
  977. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  978. | ATTR_GID | ATTR_UID | ATTR_MODE)
  979. if (!(attr->ia_valid & OCFS2_VALID_ATTRS))
  980. return 0;
  981. status = inode_change_ok(inode, attr);
  982. if (status)
  983. return status;
  984. if (is_quota_modification(inode, attr))
  985. dquot_initialize(inode);
  986. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  987. if (size_change) {
  988. status = ocfs2_rw_lock(inode, 1);
  989. if (status < 0) {
  990. mlog_errno(status);
  991. goto bail;
  992. }
  993. }
  994. status = ocfs2_inode_lock(inode, &bh, 1);
  995. if (status < 0) {
  996. if (status != -ENOENT)
  997. mlog_errno(status);
  998. goto bail_unlock_rw;
  999. }
  1000. if (size_change) {
  1001. status = inode_newsize_ok(inode, attr->ia_size);
  1002. if (status)
  1003. goto bail_unlock;
  1004. inode_dio_wait(inode);
  1005. if (i_size_read(inode) >= attr->ia_size) {
  1006. if (ocfs2_should_order_data(inode)) {
  1007. status = ocfs2_begin_ordered_truncate(inode,
  1008. attr->ia_size);
  1009. if (status)
  1010. goto bail_unlock;
  1011. }
  1012. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  1013. } else
  1014. status = ocfs2_extend_file(inode, bh, attr->ia_size);
  1015. if (status < 0) {
  1016. if (status != -ENOSPC)
  1017. mlog_errno(status);
  1018. status = -ENOSPC;
  1019. goto bail_unlock;
  1020. }
  1021. }
  1022. if ((attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
  1023. (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
  1024. /*
  1025. * Gather pointers to quota structures so that allocation /
  1026. * freeing of quota structures happens here and not inside
  1027. * dquot_transfer() where we have problems with lock ordering
  1028. */
  1029. if (attr->ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)
  1030. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1031. OCFS2_FEATURE_RO_COMPAT_USRQUOTA)) {
  1032. transfer_to[USRQUOTA] = dqget(sb, make_kqid_uid(attr->ia_uid));
  1033. if (!transfer_to[USRQUOTA]) {
  1034. status = -ESRCH;
  1035. goto bail_unlock;
  1036. }
  1037. }
  1038. if (attr->ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid)
  1039. && OCFS2_HAS_RO_COMPAT_FEATURE(sb,
  1040. OCFS2_FEATURE_RO_COMPAT_GRPQUOTA)) {
  1041. transfer_to[GRPQUOTA] = dqget(sb, make_kqid_gid(attr->ia_gid));
  1042. if (!transfer_to[GRPQUOTA]) {
  1043. status = -ESRCH;
  1044. goto bail_unlock;
  1045. }
  1046. }
  1047. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS +
  1048. 2 * ocfs2_quota_trans_credits(sb));
  1049. if (IS_ERR(handle)) {
  1050. status = PTR_ERR(handle);
  1051. mlog_errno(status);
  1052. goto bail_unlock;
  1053. }
  1054. status = __dquot_transfer(inode, transfer_to);
  1055. if (status < 0)
  1056. goto bail_commit;
  1057. } else {
  1058. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1059. if (IS_ERR(handle)) {
  1060. status = PTR_ERR(handle);
  1061. mlog_errno(status);
  1062. goto bail_unlock;
  1063. }
  1064. }
  1065. setattr_copy(inode, attr);
  1066. mark_inode_dirty(inode);
  1067. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  1068. if (status < 0)
  1069. mlog_errno(status);
  1070. bail_commit:
  1071. ocfs2_commit_trans(osb, handle);
  1072. bail_unlock:
  1073. ocfs2_inode_unlock(inode, 1);
  1074. bail_unlock_rw:
  1075. if (size_change)
  1076. ocfs2_rw_unlock(inode, 1);
  1077. bail:
  1078. brelse(bh);
  1079. /* Release quota pointers in case we acquired them */
  1080. for (qtype = 0; qtype < MAXQUOTAS; qtype++)
  1081. dqput(transfer_to[qtype]);
  1082. if (!status && attr->ia_valid & ATTR_MODE) {
  1083. status = posix_acl_chmod(inode, inode->i_mode);
  1084. if (status < 0)
  1085. mlog_errno(status);
  1086. }
  1087. return status;
  1088. }
  1089. int ocfs2_getattr(struct vfsmount *mnt,
  1090. struct dentry *dentry,
  1091. struct kstat *stat)
  1092. {
  1093. struct inode *inode = dentry->d_inode;
  1094. struct super_block *sb = dentry->d_inode->i_sb;
  1095. struct ocfs2_super *osb = sb->s_fs_info;
  1096. int err;
  1097. err = ocfs2_inode_revalidate(dentry);
  1098. if (err) {
  1099. if (err != -ENOENT)
  1100. mlog_errno(err);
  1101. goto bail;
  1102. }
  1103. generic_fillattr(inode, stat);
  1104. /* We set the blksize from the cluster size for performance */
  1105. stat->blksize = osb->s_clustersize;
  1106. bail:
  1107. return err;
  1108. }
  1109. int ocfs2_permission(struct inode *inode, int mask)
  1110. {
  1111. int ret;
  1112. if (mask & MAY_NOT_BLOCK)
  1113. return -ECHILD;
  1114. ret = ocfs2_inode_lock(inode, NULL, 0);
  1115. if (ret) {
  1116. if (ret != -ENOENT)
  1117. mlog_errno(ret);
  1118. goto out;
  1119. }
  1120. ret = generic_permission(inode, mask);
  1121. ocfs2_inode_unlock(inode, 0);
  1122. out:
  1123. return ret;
  1124. }
  1125. static int __ocfs2_write_remove_suid(struct inode *inode,
  1126. struct buffer_head *bh)
  1127. {
  1128. int ret;
  1129. handle_t *handle;
  1130. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1131. struct ocfs2_dinode *di;
  1132. trace_ocfs2_write_remove_suid(
  1133. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1134. inode->i_mode);
  1135. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1136. if (IS_ERR(handle)) {
  1137. ret = PTR_ERR(handle);
  1138. mlog_errno(ret);
  1139. goto out;
  1140. }
  1141. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh,
  1142. OCFS2_JOURNAL_ACCESS_WRITE);
  1143. if (ret < 0) {
  1144. mlog_errno(ret);
  1145. goto out_trans;
  1146. }
  1147. inode->i_mode &= ~S_ISUID;
  1148. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  1149. inode->i_mode &= ~S_ISGID;
  1150. di = (struct ocfs2_dinode *) bh->b_data;
  1151. di->i_mode = cpu_to_le16(inode->i_mode);
  1152. ocfs2_journal_dirty(handle, bh);
  1153. out_trans:
  1154. ocfs2_commit_trans(osb, handle);
  1155. out:
  1156. return ret;
  1157. }
  1158. /*
  1159. * Will look for holes and unwritten extents in the range starting at
  1160. * pos for count bytes (inclusive).
  1161. */
  1162. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  1163. size_t count)
  1164. {
  1165. int ret = 0;
  1166. unsigned int extent_flags;
  1167. u32 cpos, clusters, extent_len, phys_cpos;
  1168. struct super_block *sb = inode->i_sb;
  1169. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1170. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1171. while (clusters) {
  1172. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1173. &extent_flags);
  1174. if (ret < 0) {
  1175. mlog_errno(ret);
  1176. goto out;
  1177. }
  1178. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  1179. ret = 1;
  1180. break;
  1181. }
  1182. if (extent_len > clusters)
  1183. extent_len = clusters;
  1184. clusters -= extent_len;
  1185. cpos += extent_len;
  1186. }
  1187. out:
  1188. return ret;
  1189. }
  1190. static int ocfs2_write_remove_suid(struct inode *inode)
  1191. {
  1192. int ret;
  1193. struct buffer_head *bh = NULL;
  1194. ret = ocfs2_read_inode_block(inode, &bh);
  1195. if (ret < 0) {
  1196. mlog_errno(ret);
  1197. goto out;
  1198. }
  1199. ret = __ocfs2_write_remove_suid(inode, bh);
  1200. out:
  1201. brelse(bh);
  1202. return ret;
  1203. }
  1204. /*
  1205. * Allocate enough extents to cover the region starting at byte offset
  1206. * start for len bytes. Existing extents are skipped, any extents
  1207. * added are marked as "unwritten".
  1208. */
  1209. static int ocfs2_allocate_unwritten_extents(struct inode *inode,
  1210. u64 start, u64 len)
  1211. {
  1212. int ret;
  1213. u32 cpos, phys_cpos, clusters, alloc_size;
  1214. u64 end = start + len;
  1215. struct buffer_head *di_bh = NULL;
  1216. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1217. ret = ocfs2_read_inode_block(inode, &di_bh);
  1218. if (ret) {
  1219. mlog_errno(ret);
  1220. goto out;
  1221. }
  1222. /*
  1223. * Nothing to do if the requested reservation range
  1224. * fits within the inode.
  1225. */
  1226. if (ocfs2_size_fits_inline_data(di_bh, end))
  1227. goto out;
  1228. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  1229. if (ret) {
  1230. mlog_errno(ret);
  1231. goto out;
  1232. }
  1233. }
  1234. /*
  1235. * We consider both start and len to be inclusive.
  1236. */
  1237. cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1238. clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
  1239. clusters -= cpos;
  1240. while (clusters) {
  1241. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1242. &alloc_size, NULL);
  1243. if (ret) {
  1244. mlog_errno(ret);
  1245. goto out;
  1246. }
  1247. /*
  1248. * Hole or existing extent len can be arbitrary, so
  1249. * cap it to our own allocation request.
  1250. */
  1251. if (alloc_size > clusters)
  1252. alloc_size = clusters;
  1253. if (phys_cpos) {
  1254. /*
  1255. * We already have an allocation at this
  1256. * region so we can safely skip it.
  1257. */
  1258. goto next;
  1259. }
  1260. ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
  1261. if (ret) {
  1262. if (ret != -ENOSPC)
  1263. mlog_errno(ret);
  1264. goto out;
  1265. }
  1266. next:
  1267. cpos += alloc_size;
  1268. clusters -= alloc_size;
  1269. }
  1270. ret = 0;
  1271. out:
  1272. brelse(di_bh);
  1273. return ret;
  1274. }
  1275. /*
  1276. * Truncate a byte range, avoiding pages within partial clusters. This
  1277. * preserves those pages for the zeroing code to write to.
  1278. */
  1279. static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
  1280. u64 byte_len)
  1281. {
  1282. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1283. loff_t start, end;
  1284. struct address_space *mapping = inode->i_mapping;
  1285. start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
  1286. end = byte_start + byte_len;
  1287. end = end & ~(osb->s_clustersize - 1);
  1288. if (start < end) {
  1289. unmap_mapping_range(mapping, start, end - start, 0);
  1290. truncate_inode_pages_range(mapping, start, end - 1);
  1291. }
  1292. }
  1293. static int ocfs2_zero_partial_clusters(struct inode *inode,
  1294. u64 start, u64 len)
  1295. {
  1296. int ret = 0;
  1297. u64 tmpend, end = start + len;
  1298. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1299. unsigned int csize = osb->s_clustersize;
  1300. handle_t *handle;
  1301. /*
  1302. * The "start" and "end" values are NOT necessarily part of
  1303. * the range whose allocation is being deleted. Rather, this
  1304. * is what the user passed in with the request. We must zero
  1305. * partial clusters here. There's no need to worry about
  1306. * physical allocation - the zeroing code knows to skip holes.
  1307. */
  1308. trace_ocfs2_zero_partial_clusters(
  1309. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1310. (unsigned long long)start, (unsigned long long)end);
  1311. /*
  1312. * If both edges are on a cluster boundary then there's no
  1313. * zeroing required as the region is part of the allocation to
  1314. * be truncated.
  1315. */
  1316. if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
  1317. goto out;
  1318. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1319. if (IS_ERR(handle)) {
  1320. ret = PTR_ERR(handle);
  1321. mlog_errno(ret);
  1322. goto out;
  1323. }
  1324. /*
  1325. * We want to get the byte offset of the end of the 1st cluster.
  1326. */
  1327. tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
  1328. if (tmpend > end)
  1329. tmpend = end;
  1330. trace_ocfs2_zero_partial_clusters_range1((unsigned long long)start,
  1331. (unsigned long long)tmpend);
  1332. ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
  1333. if (ret)
  1334. mlog_errno(ret);
  1335. if (tmpend < end) {
  1336. /*
  1337. * This may make start and end equal, but the zeroing
  1338. * code will skip any work in that case so there's no
  1339. * need to catch it up here.
  1340. */
  1341. start = end & ~(osb->s_clustersize - 1);
  1342. trace_ocfs2_zero_partial_clusters_range2(
  1343. (unsigned long long)start, (unsigned long long)end);
  1344. ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
  1345. if (ret)
  1346. mlog_errno(ret);
  1347. }
  1348. ocfs2_commit_trans(osb, handle);
  1349. out:
  1350. return ret;
  1351. }
  1352. static int ocfs2_find_rec(struct ocfs2_extent_list *el, u32 pos)
  1353. {
  1354. int i;
  1355. struct ocfs2_extent_rec *rec = NULL;
  1356. for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
  1357. rec = &el->l_recs[i];
  1358. if (le32_to_cpu(rec->e_cpos) < pos)
  1359. break;
  1360. }
  1361. return i;
  1362. }
  1363. /*
  1364. * Helper to calculate the punching pos and length in one run, we handle the
  1365. * following three cases in order:
  1366. *
  1367. * - remove the entire record
  1368. * - remove a partial record
  1369. * - no record needs to be removed (hole-punching completed)
  1370. */
  1371. static void ocfs2_calc_trunc_pos(struct inode *inode,
  1372. struct ocfs2_extent_list *el,
  1373. struct ocfs2_extent_rec *rec,
  1374. u32 trunc_start, u32 *trunc_cpos,
  1375. u32 *trunc_len, u32 *trunc_end,
  1376. u64 *blkno, int *done)
  1377. {
  1378. int ret = 0;
  1379. u32 coff, range;
  1380. range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
  1381. if (le32_to_cpu(rec->e_cpos) >= trunc_start) {
  1382. /*
  1383. * remove an entire extent record.
  1384. */
  1385. *trunc_cpos = le32_to_cpu(rec->e_cpos);
  1386. /*
  1387. * Skip holes if any.
  1388. */
  1389. if (range < *trunc_end)
  1390. *trunc_end = range;
  1391. *trunc_len = *trunc_end - le32_to_cpu(rec->e_cpos);
  1392. *blkno = le64_to_cpu(rec->e_blkno);
  1393. *trunc_end = le32_to_cpu(rec->e_cpos);
  1394. } else if (range > trunc_start) {
  1395. /*
  1396. * remove a partial extent record, which means we're
  1397. * removing the last extent record.
  1398. */
  1399. *trunc_cpos = trunc_start;
  1400. /*
  1401. * skip hole if any.
  1402. */
  1403. if (range < *trunc_end)
  1404. *trunc_end = range;
  1405. *trunc_len = *trunc_end - trunc_start;
  1406. coff = trunc_start - le32_to_cpu(rec->e_cpos);
  1407. *blkno = le64_to_cpu(rec->e_blkno) +
  1408. ocfs2_clusters_to_blocks(inode->i_sb, coff);
  1409. *trunc_end = trunc_start;
  1410. } else {
  1411. /*
  1412. * It may have two following possibilities:
  1413. *
  1414. * - last record has been removed
  1415. * - trunc_start was within a hole
  1416. *
  1417. * both two cases mean the completion of hole punching.
  1418. */
  1419. ret = 1;
  1420. }
  1421. *done = ret;
  1422. }
  1423. static int ocfs2_remove_inode_range(struct inode *inode,
  1424. struct buffer_head *di_bh, u64 byte_start,
  1425. u64 byte_len)
  1426. {
  1427. int ret = 0, flags = 0, done = 0, i;
  1428. u32 trunc_start, trunc_len, trunc_end, trunc_cpos, phys_cpos;
  1429. u32 cluster_in_el;
  1430. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1431. struct ocfs2_cached_dealloc_ctxt dealloc;
  1432. struct address_space *mapping = inode->i_mapping;
  1433. struct ocfs2_extent_tree et;
  1434. struct ocfs2_path *path = NULL;
  1435. struct ocfs2_extent_list *el = NULL;
  1436. struct ocfs2_extent_rec *rec = NULL;
  1437. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1438. u64 blkno, refcount_loc = le64_to_cpu(di->i_refcount_loc);
  1439. ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
  1440. ocfs2_init_dealloc_ctxt(&dealloc);
  1441. trace_ocfs2_remove_inode_range(
  1442. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1443. (unsigned long long)byte_start,
  1444. (unsigned long long)byte_len);
  1445. if (byte_len == 0)
  1446. return 0;
  1447. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1448. ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
  1449. byte_start + byte_len, 0);
  1450. if (ret) {
  1451. mlog_errno(ret);
  1452. goto out;
  1453. }
  1454. /*
  1455. * There's no need to get fancy with the page cache
  1456. * truncate of an inline-data inode. We're talking
  1457. * about less than a page here, which will be cached
  1458. * in the dinode buffer anyway.
  1459. */
  1460. unmap_mapping_range(mapping, 0, 0, 0);
  1461. truncate_inode_pages(mapping, 0);
  1462. goto out;
  1463. }
  1464. /*
  1465. * For reflinks, we may need to CoW 2 clusters which might be
  1466. * partially zero'd later, if hole's start and end offset were
  1467. * within one cluster(means is not exactly aligned to clustersize).
  1468. */
  1469. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) {
  1470. ret = ocfs2_cow_file_pos(inode, di_bh, byte_start);
  1471. if (ret) {
  1472. mlog_errno(ret);
  1473. goto out;
  1474. }
  1475. ret = ocfs2_cow_file_pos(inode, di_bh, byte_start + byte_len);
  1476. if (ret) {
  1477. mlog_errno(ret);
  1478. goto out;
  1479. }
  1480. }
  1481. trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
  1482. trunc_end = (byte_start + byte_len) >> osb->s_clustersize_bits;
  1483. cluster_in_el = trunc_end;
  1484. ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
  1485. if (ret) {
  1486. mlog_errno(ret);
  1487. goto out;
  1488. }
  1489. path = ocfs2_new_path_from_et(&et);
  1490. if (!path) {
  1491. ret = -ENOMEM;
  1492. mlog_errno(ret);
  1493. goto out;
  1494. }
  1495. while (trunc_end > trunc_start) {
  1496. ret = ocfs2_find_path(INODE_CACHE(inode), path,
  1497. cluster_in_el);
  1498. if (ret) {
  1499. mlog_errno(ret);
  1500. goto out;
  1501. }
  1502. el = path_leaf_el(path);
  1503. i = ocfs2_find_rec(el, trunc_end);
  1504. /*
  1505. * Need to go to previous extent block.
  1506. */
  1507. if (i < 0) {
  1508. if (path->p_tree_depth == 0)
  1509. break;
  1510. ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
  1511. path,
  1512. &cluster_in_el);
  1513. if (ret) {
  1514. mlog_errno(ret);
  1515. goto out;
  1516. }
  1517. /*
  1518. * We've reached the leftmost extent block,
  1519. * it's safe to leave.
  1520. */
  1521. if (cluster_in_el == 0)
  1522. break;
  1523. /*
  1524. * The 'pos' searched for previous extent block is
  1525. * always one cluster less than actual trunc_end.
  1526. */
  1527. trunc_end = cluster_in_el + 1;
  1528. ocfs2_reinit_path(path, 1);
  1529. continue;
  1530. } else
  1531. rec = &el->l_recs[i];
  1532. ocfs2_calc_trunc_pos(inode, el, rec, trunc_start, &trunc_cpos,
  1533. &trunc_len, &trunc_end, &blkno, &done);
  1534. if (done)
  1535. break;
  1536. flags = rec->e_flags;
  1537. phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
  1538. ret = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
  1539. phys_cpos, trunc_len, flags,
  1540. &dealloc, refcount_loc);
  1541. if (ret < 0) {
  1542. mlog_errno(ret);
  1543. goto out;
  1544. }
  1545. cluster_in_el = trunc_end;
  1546. ocfs2_reinit_path(path, 1);
  1547. }
  1548. ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
  1549. out:
  1550. ocfs2_free_path(path);
  1551. ocfs2_schedule_truncate_log_flush(osb, 1);
  1552. ocfs2_run_deallocs(osb, &dealloc);
  1553. return ret;
  1554. }
  1555. /*
  1556. * Parts of this function taken from xfs_change_file_space()
  1557. */
  1558. static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  1559. loff_t f_pos, unsigned int cmd,
  1560. struct ocfs2_space_resv *sr,
  1561. int change_size)
  1562. {
  1563. int ret;
  1564. s64 llen;
  1565. loff_t size;
  1566. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1567. struct buffer_head *di_bh = NULL;
  1568. handle_t *handle;
  1569. unsigned long long max_off = inode->i_sb->s_maxbytes;
  1570. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  1571. return -EROFS;
  1572. mutex_lock(&inode->i_mutex);
  1573. /*
  1574. * This prevents concurrent writes on other nodes
  1575. */
  1576. ret = ocfs2_rw_lock(inode, 1);
  1577. if (ret) {
  1578. mlog_errno(ret);
  1579. goto out;
  1580. }
  1581. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1582. if (ret) {
  1583. mlog_errno(ret);
  1584. goto out_rw_unlock;
  1585. }
  1586. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  1587. ret = -EPERM;
  1588. goto out_inode_unlock;
  1589. }
  1590. switch (sr->l_whence) {
  1591. case 0: /*SEEK_SET*/
  1592. break;
  1593. case 1: /*SEEK_CUR*/
  1594. sr->l_start += f_pos;
  1595. break;
  1596. case 2: /*SEEK_END*/
  1597. sr->l_start += i_size_read(inode);
  1598. break;
  1599. default:
  1600. ret = -EINVAL;
  1601. goto out_inode_unlock;
  1602. }
  1603. sr->l_whence = 0;
  1604. llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
  1605. if (sr->l_start < 0
  1606. || sr->l_start > max_off
  1607. || (sr->l_start + llen) < 0
  1608. || (sr->l_start + llen) > max_off) {
  1609. ret = -EINVAL;
  1610. goto out_inode_unlock;
  1611. }
  1612. size = sr->l_start + sr->l_len;
  1613. if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64 ||
  1614. cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) {
  1615. if (sr->l_len <= 0) {
  1616. ret = -EINVAL;
  1617. goto out_inode_unlock;
  1618. }
  1619. }
  1620. if (file && should_remove_suid(file->f_path.dentry)) {
  1621. ret = __ocfs2_write_remove_suid(inode, di_bh);
  1622. if (ret) {
  1623. mlog_errno(ret);
  1624. goto out_inode_unlock;
  1625. }
  1626. }
  1627. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1628. switch (cmd) {
  1629. case OCFS2_IOC_RESVSP:
  1630. case OCFS2_IOC_RESVSP64:
  1631. /*
  1632. * This takes unsigned offsets, but the signed ones we
  1633. * pass have been checked against overflow above.
  1634. */
  1635. ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
  1636. sr->l_len);
  1637. break;
  1638. case OCFS2_IOC_UNRESVSP:
  1639. case OCFS2_IOC_UNRESVSP64:
  1640. ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
  1641. sr->l_len);
  1642. break;
  1643. default:
  1644. ret = -EINVAL;
  1645. }
  1646. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1647. if (ret) {
  1648. mlog_errno(ret);
  1649. goto out_inode_unlock;
  1650. }
  1651. /*
  1652. * We update c/mtime for these changes
  1653. */
  1654. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1655. if (IS_ERR(handle)) {
  1656. ret = PTR_ERR(handle);
  1657. mlog_errno(ret);
  1658. goto out_inode_unlock;
  1659. }
  1660. if (change_size && i_size_read(inode) < size)
  1661. i_size_write(inode, size);
  1662. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1663. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1664. if (ret < 0)
  1665. mlog_errno(ret);
  1666. if (file && (file->f_flags & O_SYNC))
  1667. handle->h_sync = 1;
  1668. ocfs2_commit_trans(osb, handle);
  1669. out_inode_unlock:
  1670. brelse(di_bh);
  1671. ocfs2_inode_unlock(inode, 1);
  1672. out_rw_unlock:
  1673. ocfs2_rw_unlock(inode, 1);
  1674. out:
  1675. mutex_unlock(&inode->i_mutex);
  1676. return ret;
  1677. }
  1678. int ocfs2_change_file_space(struct file *file, unsigned int cmd,
  1679. struct ocfs2_space_resv *sr)
  1680. {
  1681. struct inode *inode = file_inode(file);
  1682. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1683. int ret;
  1684. if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
  1685. !ocfs2_writes_unwritten_extents(osb))
  1686. return -ENOTTY;
  1687. else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
  1688. !ocfs2_sparse_alloc(osb))
  1689. return -ENOTTY;
  1690. if (!S_ISREG(inode->i_mode))
  1691. return -EINVAL;
  1692. if (!(file->f_mode & FMODE_WRITE))
  1693. return -EBADF;
  1694. ret = mnt_want_write_file(file);
  1695. if (ret)
  1696. return ret;
  1697. ret = __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
  1698. mnt_drop_write_file(file);
  1699. return ret;
  1700. }
  1701. static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
  1702. loff_t len)
  1703. {
  1704. struct inode *inode = file_inode(file);
  1705. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1706. struct ocfs2_space_resv sr;
  1707. int change_size = 1;
  1708. int cmd = OCFS2_IOC_RESVSP64;
  1709. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  1710. return -EOPNOTSUPP;
  1711. if (!ocfs2_writes_unwritten_extents(osb))
  1712. return -EOPNOTSUPP;
  1713. if (mode & FALLOC_FL_KEEP_SIZE)
  1714. change_size = 0;
  1715. if (mode & FALLOC_FL_PUNCH_HOLE)
  1716. cmd = OCFS2_IOC_UNRESVSP64;
  1717. sr.l_whence = 0;
  1718. sr.l_start = (s64)offset;
  1719. sr.l_len = (s64)len;
  1720. return __ocfs2_change_file_space(NULL, inode, offset, cmd, &sr,
  1721. change_size);
  1722. }
  1723. int ocfs2_check_range_for_refcount(struct inode *inode, loff_t pos,
  1724. size_t count)
  1725. {
  1726. int ret = 0;
  1727. unsigned int extent_flags;
  1728. u32 cpos, clusters, extent_len, phys_cpos;
  1729. struct super_block *sb = inode->i_sb;
  1730. if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)) ||
  1731. !(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL) ||
  1732. OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  1733. return 0;
  1734. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  1735. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  1736. while (clusters) {
  1737. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  1738. &extent_flags);
  1739. if (ret < 0) {
  1740. mlog_errno(ret);
  1741. goto out;
  1742. }
  1743. if (phys_cpos && (extent_flags & OCFS2_EXT_REFCOUNTED)) {
  1744. ret = 1;
  1745. break;
  1746. }
  1747. if (extent_len > clusters)
  1748. extent_len = clusters;
  1749. clusters -= extent_len;
  1750. cpos += extent_len;
  1751. }
  1752. out:
  1753. return ret;
  1754. }
  1755. static void ocfs2_aiodio_wait(struct inode *inode)
  1756. {
  1757. wait_queue_head_t *wq = ocfs2_ioend_wq(inode);
  1758. wait_event(*wq, (atomic_read(&OCFS2_I(inode)->ip_unaligned_aio) == 0));
  1759. }
  1760. static int ocfs2_is_io_unaligned(struct inode *inode, size_t count, loff_t pos)
  1761. {
  1762. int blockmask = inode->i_sb->s_blocksize - 1;
  1763. loff_t final_size = pos + count;
  1764. if ((pos & blockmask) || (final_size & blockmask))
  1765. return 1;
  1766. return 0;
  1767. }
  1768. static int ocfs2_prepare_inode_for_refcount(struct inode *inode,
  1769. struct file *file,
  1770. loff_t pos, size_t count,
  1771. int *meta_level)
  1772. {
  1773. int ret;
  1774. struct buffer_head *di_bh = NULL;
  1775. u32 cpos = pos >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1776. u32 clusters =
  1777. ocfs2_clusters_for_bytes(inode->i_sb, pos + count) - cpos;
  1778. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1779. if (ret) {
  1780. mlog_errno(ret);
  1781. goto out;
  1782. }
  1783. *meta_level = 1;
  1784. ret = ocfs2_refcount_cow(inode, di_bh, cpos, clusters, UINT_MAX);
  1785. if (ret)
  1786. mlog_errno(ret);
  1787. out:
  1788. brelse(di_bh);
  1789. return ret;
  1790. }
  1791. static int ocfs2_prepare_inode_for_write(struct file *file,
  1792. loff_t *ppos,
  1793. size_t count,
  1794. int appending,
  1795. int *direct_io,
  1796. int *has_refcount)
  1797. {
  1798. int ret = 0, meta_level = 0;
  1799. struct dentry *dentry = file->f_path.dentry;
  1800. struct inode *inode = dentry->d_inode;
  1801. loff_t saved_pos = 0, end;
  1802. /*
  1803. * We start with a read level meta lock and only jump to an ex
  1804. * if we need to make modifications here.
  1805. */
  1806. for(;;) {
  1807. ret = ocfs2_inode_lock(inode, NULL, meta_level);
  1808. if (ret < 0) {
  1809. meta_level = -1;
  1810. mlog_errno(ret);
  1811. goto out;
  1812. }
  1813. /* Clear suid / sgid if necessary. We do this here
  1814. * instead of later in the write path because
  1815. * remove_suid() calls ->setattr without any hint that
  1816. * we may have already done our cluster locking. Since
  1817. * ocfs2_setattr() *must* take cluster locks to
  1818. * proceed, this will lead us to recursively lock the
  1819. * inode. There's also the dinode i_size state which
  1820. * can be lost via setattr during extending writes (we
  1821. * set inode->i_size at the end of a write. */
  1822. if (should_remove_suid(dentry)) {
  1823. if (meta_level == 0) {
  1824. ocfs2_inode_unlock(inode, meta_level);
  1825. meta_level = 1;
  1826. continue;
  1827. }
  1828. ret = ocfs2_write_remove_suid(inode);
  1829. if (ret < 0) {
  1830. mlog_errno(ret);
  1831. goto out_unlock;
  1832. }
  1833. }
  1834. /* work on a copy of ppos until we're sure that we won't have
  1835. * to recalculate it due to relocking. */
  1836. if (appending)
  1837. saved_pos = i_size_read(inode);
  1838. else
  1839. saved_pos = *ppos;
  1840. end = saved_pos + count;
  1841. ret = ocfs2_check_range_for_refcount(inode, saved_pos, count);
  1842. if (ret == 1) {
  1843. ocfs2_inode_unlock(inode, meta_level);
  1844. meta_level = -1;
  1845. ret = ocfs2_prepare_inode_for_refcount(inode,
  1846. file,
  1847. saved_pos,
  1848. count,
  1849. &meta_level);
  1850. if (has_refcount)
  1851. *has_refcount = 1;
  1852. if (direct_io)
  1853. *direct_io = 0;
  1854. }
  1855. if (ret < 0) {
  1856. mlog_errno(ret);
  1857. goto out_unlock;
  1858. }
  1859. /*
  1860. * Skip the O_DIRECT checks if we don't need
  1861. * them.
  1862. */
  1863. if (!direct_io || !(*direct_io))
  1864. break;
  1865. /*
  1866. * There's no sane way to do direct writes to an inode
  1867. * with inline data.
  1868. */
  1869. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1870. *direct_io = 0;
  1871. break;
  1872. }
  1873. /*
  1874. * Allowing concurrent direct writes means
  1875. * i_size changes wouldn't be synchronized, so
  1876. * one node could wind up truncating another
  1877. * nodes writes.
  1878. */
  1879. if (end > i_size_read(inode)) {
  1880. *direct_io = 0;
  1881. break;
  1882. }
  1883. /*
  1884. * We don't fill holes during direct io, so
  1885. * check for them here. If any are found, the
  1886. * caller will have to retake some cluster
  1887. * locks and initiate the io as buffered.
  1888. */
  1889. ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
  1890. if (ret == 1) {
  1891. *direct_io = 0;
  1892. ret = 0;
  1893. } else if (ret < 0)
  1894. mlog_errno(ret);
  1895. break;
  1896. }
  1897. if (appending)
  1898. *ppos = saved_pos;
  1899. out_unlock:
  1900. trace_ocfs2_prepare_inode_for_write(OCFS2_I(inode)->ip_blkno,
  1901. saved_pos, appending, count,
  1902. direct_io, has_refcount);
  1903. if (meta_level >= 0)
  1904. ocfs2_inode_unlock(inode, meta_level);
  1905. out:
  1906. return ret;
  1907. }
  1908. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1909. const struct iovec *iov,
  1910. unsigned long nr_segs,
  1911. loff_t pos)
  1912. {
  1913. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1914. int can_do_direct, has_refcount = 0;
  1915. ssize_t written = 0;
  1916. size_t ocount; /* original count */
  1917. size_t count; /* after file limit checks */
  1918. loff_t old_size, *ppos = &iocb->ki_pos;
  1919. u32 old_clusters;
  1920. struct file *file = iocb->ki_filp;
  1921. struct inode *inode = file_inode(file);
  1922. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1923. int full_coherency = !(osb->s_mount_opt &
  1924. OCFS2_MOUNT_COHERENCY_BUFFERED);
  1925. int unaligned_dio = 0;
  1926. trace_ocfs2_file_aio_write(inode, file, file->f_path.dentry,
  1927. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1928. file->f_path.dentry->d_name.len,
  1929. file->f_path.dentry->d_name.name,
  1930. (unsigned int)nr_segs);
  1931. if (iocb->ki_nbytes == 0)
  1932. return 0;
  1933. appending = file->f_flags & O_APPEND ? 1 : 0;
  1934. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1935. mutex_lock(&inode->i_mutex);
  1936. ocfs2_iocb_clear_sem_locked(iocb);
  1937. relock:
  1938. /* to match setattr's i_mutex -> rw_lock ordering */
  1939. if (direct_io) {
  1940. have_alloc_sem = 1;
  1941. /* communicate with ocfs2_dio_end_io */
  1942. ocfs2_iocb_set_sem_locked(iocb);
  1943. }
  1944. /*
  1945. * Concurrent O_DIRECT writes are allowed with
  1946. * mount_option "coherency=buffered".
  1947. */
  1948. rw_level = (!direct_io || full_coherency);
  1949. ret = ocfs2_rw_lock(inode, rw_level);
  1950. if (ret < 0) {
  1951. mlog_errno(ret);
  1952. goto out_sems;
  1953. }
  1954. /*
  1955. * O_DIRECT writes with "coherency=full" need to take EX cluster
  1956. * inode_lock to guarantee coherency.
  1957. */
  1958. if (direct_io && full_coherency) {
  1959. /*
  1960. * We need to take and drop the inode lock to force
  1961. * other nodes to drop their caches. Buffered I/O
  1962. * already does this in write_begin().
  1963. */
  1964. ret = ocfs2_inode_lock(inode, NULL, 1);
  1965. if (ret < 0) {
  1966. mlog_errno(ret);
  1967. goto out;
  1968. }
  1969. ocfs2_inode_unlock(inode, 1);
  1970. }
  1971. can_do_direct = direct_io;
  1972. ret = ocfs2_prepare_inode_for_write(file, ppos,
  1973. iocb->ki_nbytes, appending,
  1974. &can_do_direct, &has_refcount);
  1975. if (ret < 0) {
  1976. mlog_errno(ret);
  1977. goto out;
  1978. }
  1979. if (direct_io && !is_sync_kiocb(iocb))
  1980. unaligned_dio = ocfs2_is_io_unaligned(inode, iocb->ki_nbytes,
  1981. *ppos);
  1982. /*
  1983. * We can't complete the direct I/O as requested, fall back to
  1984. * buffered I/O.
  1985. */
  1986. if (direct_io && !can_do_direct) {
  1987. ocfs2_rw_unlock(inode, rw_level);
  1988. have_alloc_sem = 0;
  1989. rw_level = -1;
  1990. direct_io = 0;
  1991. goto relock;
  1992. }
  1993. if (unaligned_dio) {
  1994. /*
  1995. * Wait on previous unaligned aio to complete before
  1996. * proceeding.
  1997. */
  1998. ocfs2_aiodio_wait(inode);
  1999. /* Mark the iocb as needing a decrement in ocfs2_dio_end_io */
  2000. atomic_inc(&OCFS2_I(inode)->ip_unaligned_aio);
  2001. ocfs2_iocb_set_unaligned_aio(iocb);
  2002. }
  2003. /*
  2004. * To later detect whether a journal commit for sync writes is
  2005. * necessary, we sample i_size, and cluster count here.
  2006. */
  2007. old_size = i_size_read(inode);
  2008. old_clusters = OCFS2_I(inode)->ip_clusters;
  2009. /* communicate with ocfs2_dio_end_io */
  2010. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  2011. ret = generic_segment_checks(iov, &nr_segs, &ocount,
  2012. VERIFY_READ);
  2013. if (ret)
  2014. goto out_dio;
  2015. count = ocount;
  2016. ret = generic_write_checks(file, ppos, &count,
  2017. S_ISBLK(inode->i_mode));
  2018. if (ret)
  2019. goto out_dio;
  2020. if (direct_io) {
  2021. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  2022. ppos, count, ocount);
  2023. if (written < 0) {
  2024. ret = written;
  2025. goto out_dio;
  2026. }
  2027. } else {
  2028. current->backing_dev_info = file->f_mapping->backing_dev_info;
  2029. written = generic_file_buffered_write(iocb, iov, nr_segs, *ppos,
  2030. ppos, count, 0);
  2031. current->backing_dev_info = NULL;
  2032. }
  2033. out_dio:
  2034. /* buffered aio wouldn't have proper lock coverage today */
  2035. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  2036. if (((file->f_flags & O_DSYNC) && !direct_io) || IS_SYNC(inode) ||
  2037. ((file->f_flags & O_DIRECT) && !direct_io)) {
  2038. ret = filemap_fdatawrite_range(file->f_mapping, *ppos,
  2039. *ppos + count - 1);
  2040. if (ret < 0)
  2041. written = ret;
  2042. if (!ret && ((old_size != i_size_read(inode)) ||
  2043. (old_clusters != OCFS2_I(inode)->ip_clusters) ||
  2044. has_refcount)) {
  2045. ret = jbd2_journal_force_commit(osb->journal->j_journal);
  2046. if (ret < 0)
  2047. written = ret;
  2048. }
  2049. if (!ret)
  2050. ret = filemap_fdatawait_range(file->f_mapping, *ppos,
  2051. *ppos + count - 1);
  2052. }
  2053. /*
  2054. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  2055. * function pointer which is called when o_direct io completes so that
  2056. * it can unlock our rw lock.
  2057. * Unfortunately there are error cases which call end_io and others
  2058. * that don't. so we don't have to unlock the rw_lock if either an
  2059. * async dio is going to do it in the future or an end_io after an
  2060. * error has already done it.
  2061. */
  2062. if ((ret == -EIOCBQUEUED) || (!ocfs2_iocb_is_rw_locked(iocb))) {
  2063. rw_level = -1;
  2064. have_alloc_sem = 0;
  2065. unaligned_dio = 0;
  2066. }
  2067. if (unaligned_dio) {
  2068. ocfs2_iocb_clear_unaligned_aio(iocb);
  2069. atomic_dec(&OCFS2_I(inode)->ip_unaligned_aio);
  2070. }
  2071. out:
  2072. if (rw_level != -1)
  2073. ocfs2_rw_unlock(inode, rw_level);
  2074. out_sems:
  2075. if (have_alloc_sem)
  2076. ocfs2_iocb_clear_sem_locked(iocb);
  2077. mutex_unlock(&inode->i_mutex);
  2078. if (written)
  2079. ret = written;
  2080. return ret;
  2081. }
  2082. static int ocfs2_splice_to_file(struct pipe_inode_info *pipe,
  2083. struct file *out,
  2084. struct splice_desc *sd)
  2085. {
  2086. int ret;
  2087. ret = ocfs2_prepare_inode_for_write(out, &sd->pos,
  2088. sd->total_len, 0, NULL, NULL);
  2089. if (ret < 0) {
  2090. mlog_errno(ret);
  2091. return ret;
  2092. }
  2093. return splice_from_pipe_feed(pipe, sd, pipe_to_file);
  2094. }
  2095. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  2096. struct file *out,
  2097. loff_t *ppos,
  2098. size_t len,
  2099. unsigned int flags)
  2100. {
  2101. int ret;
  2102. struct address_space *mapping = out->f_mapping;
  2103. struct inode *inode = mapping->host;
  2104. struct splice_desc sd = {
  2105. .total_len = len,
  2106. .flags = flags,
  2107. .pos = *ppos,
  2108. .u.file = out,
  2109. };
  2110. trace_ocfs2_file_splice_write(inode, out, out->f_path.dentry,
  2111. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2112. out->f_path.dentry->d_name.len,
  2113. out->f_path.dentry->d_name.name, len);
  2114. pipe_lock(pipe);
  2115. splice_from_pipe_begin(&sd);
  2116. do {
  2117. ret = splice_from_pipe_next(pipe, &sd);
  2118. if (ret <= 0)
  2119. break;
  2120. mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
  2121. ret = ocfs2_rw_lock(inode, 1);
  2122. if (ret < 0)
  2123. mlog_errno(ret);
  2124. else {
  2125. ret = ocfs2_splice_to_file(pipe, out, &sd);
  2126. ocfs2_rw_unlock(inode, 1);
  2127. }
  2128. mutex_unlock(&inode->i_mutex);
  2129. } while (ret > 0);
  2130. splice_from_pipe_end(pipe, &sd);
  2131. pipe_unlock(pipe);
  2132. if (sd.num_spliced)
  2133. ret = sd.num_spliced;
  2134. if (ret > 0) {
  2135. int err;
  2136. err = generic_write_sync(out, *ppos, ret);
  2137. if (err)
  2138. ret = err;
  2139. else
  2140. *ppos += ret;
  2141. balance_dirty_pages_ratelimited(mapping);
  2142. }
  2143. return ret;
  2144. }
  2145. static ssize_t ocfs2_file_splice_read(struct file *in,
  2146. loff_t *ppos,
  2147. struct pipe_inode_info *pipe,
  2148. size_t len,
  2149. unsigned int flags)
  2150. {
  2151. int ret = 0, lock_level = 0;
  2152. struct inode *inode = file_inode(in);
  2153. trace_ocfs2_file_splice_read(inode, in, in->f_path.dentry,
  2154. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2155. in->f_path.dentry->d_name.len,
  2156. in->f_path.dentry->d_name.name, len);
  2157. /*
  2158. * See the comment in ocfs2_file_aio_read()
  2159. */
  2160. ret = ocfs2_inode_lock_atime(inode, in->f_path.mnt, &lock_level);
  2161. if (ret < 0) {
  2162. mlog_errno(ret);
  2163. goto bail;
  2164. }
  2165. ocfs2_inode_unlock(inode, lock_level);
  2166. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  2167. bail:
  2168. return ret;
  2169. }
  2170. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  2171. const struct iovec *iov,
  2172. unsigned long nr_segs,
  2173. loff_t pos)
  2174. {
  2175. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  2176. struct file *filp = iocb->ki_filp;
  2177. struct inode *inode = file_inode(filp);
  2178. trace_ocfs2_file_aio_read(inode, filp, filp->f_path.dentry,
  2179. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  2180. filp->f_path.dentry->d_name.len,
  2181. filp->f_path.dentry->d_name.name, nr_segs);
  2182. if (!inode) {
  2183. ret = -EINVAL;
  2184. mlog_errno(ret);
  2185. goto bail;
  2186. }
  2187. ocfs2_iocb_clear_sem_locked(iocb);
  2188. /*
  2189. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  2190. * need locks to protect pending reads from racing with truncate.
  2191. */
  2192. if (filp->f_flags & O_DIRECT) {
  2193. have_alloc_sem = 1;
  2194. ocfs2_iocb_set_sem_locked(iocb);
  2195. ret = ocfs2_rw_lock(inode, 0);
  2196. if (ret < 0) {
  2197. mlog_errno(ret);
  2198. goto bail;
  2199. }
  2200. rw_level = 0;
  2201. /* communicate with ocfs2_dio_end_io */
  2202. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  2203. }
  2204. /*
  2205. * We're fine letting folks race truncates and extending
  2206. * writes with read across the cluster, just like they can
  2207. * locally. Hence no rw_lock during read.
  2208. *
  2209. * Take and drop the meta data lock to update inode fields
  2210. * like i_size. This allows the checks down below
  2211. * generic_file_aio_read() a chance of actually working.
  2212. */
  2213. ret = ocfs2_inode_lock_atime(inode, filp->f_path.mnt, &lock_level);
  2214. if (ret < 0) {
  2215. mlog_errno(ret);
  2216. goto bail;
  2217. }
  2218. ocfs2_inode_unlock(inode, lock_level);
  2219. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  2220. trace_generic_file_aio_read_ret(ret);
  2221. /* buffered aio wouldn't have proper lock coverage today */
  2222. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  2223. /* see ocfs2_file_aio_write */
  2224. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  2225. rw_level = -1;
  2226. have_alloc_sem = 0;
  2227. }
  2228. bail:
  2229. if (have_alloc_sem)
  2230. ocfs2_iocb_clear_sem_locked(iocb);
  2231. if (rw_level != -1)
  2232. ocfs2_rw_unlock(inode, rw_level);
  2233. return ret;
  2234. }
  2235. /* Refer generic_file_llseek_unlocked() */
  2236. static loff_t ocfs2_file_llseek(struct file *file, loff_t offset, int whence)
  2237. {
  2238. struct inode *inode = file->f_mapping->host;
  2239. int ret = 0;
  2240. mutex_lock(&inode->i_mutex);
  2241. switch (whence) {
  2242. case SEEK_SET:
  2243. break;
  2244. case SEEK_END:
  2245. offset += inode->i_size;
  2246. break;
  2247. case SEEK_CUR:
  2248. if (offset == 0) {
  2249. offset = file->f_pos;
  2250. goto out;
  2251. }
  2252. offset += file->f_pos;
  2253. break;
  2254. case SEEK_DATA:
  2255. case SEEK_HOLE:
  2256. ret = ocfs2_seek_data_hole_offset(file, &offset, whence);
  2257. if (ret)
  2258. goto out;
  2259. break;
  2260. default:
  2261. ret = -EINVAL;
  2262. goto out;
  2263. }
  2264. offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
  2265. out:
  2266. mutex_unlock(&inode->i_mutex);
  2267. if (ret)
  2268. return ret;
  2269. return offset;
  2270. }
  2271. const struct inode_operations ocfs2_file_iops = {
  2272. .setattr = ocfs2_setattr,
  2273. .getattr = ocfs2_getattr,
  2274. .permission = ocfs2_permission,
  2275. .setxattr = generic_setxattr,
  2276. .getxattr = generic_getxattr,
  2277. .listxattr = ocfs2_listxattr,
  2278. .removexattr = generic_removexattr,
  2279. .fiemap = ocfs2_fiemap,
  2280. .get_acl = ocfs2_iop_get_acl,
  2281. .set_acl = ocfs2_iop_set_acl,
  2282. };
  2283. const struct inode_operations ocfs2_special_file_iops = {
  2284. .setattr = ocfs2_setattr,
  2285. .getattr = ocfs2_getattr,
  2286. .permission = ocfs2_permission,
  2287. .get_acl = ocfs2_iop_get_acl,
  2288. .set_acl = ocfs2_iop_set_acl,
  2289. };
  2290. /*
  2291. * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
  2292. * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
  2293. */
  2294. const struct file_operations ocfs2_fops = {
  2295. .llseek = ocfs2_file_llseek,
  2296. .read = do_sync_read,
  2297. .write = do_sync_write,
  2298. .mmap = ocfs2_mmap,
  2299. .fsync = ocfs2_sync_file,
  2300. .release = ocfs2_file_release,
  2301. .open = ocfs2_file_open,
  2302. .aio_read = ocfs2_file_aio_read,
  2303. .aio_write = ocfs2_file_aio_write,
  2304. .unlocked_ioctl = ocfs2_ioctl,
  2305. #ifdef CONFIG_COMPAT
  2306. .compat_ioctl = ocfs2_compat_ioctl,
  2307. #endif
  2308. .lock = ocfs2_lock,
  2309. .flock = ocfs2_flock,
  2310. .splice_read = ocfs2_file_splice_read,
  2311. .splice_write = ocfs2_file_splice_write,
  2312. .fallocate = ocfs2_fallocate,
  2313. };
  2314. const struct file_operations ocfs2_dops = {
  2315. .llseek = generic_file_llseek,
  2316. .read = generic_read_dir,
  2317. .iterate = ocfs2_readdir,
  2318. .fsync = ocfs2_sync_file,
  2319. .release = ocfs2_dir_release,
  2320. .open = ocfs2_dir_open,
  2321. .unlocked_ioctl = ocfs2_ioctl,
  2322. #ifdef CONFIG_COMPAT
  2323. .compat_ioctl = ocfs2_compat_ioctl,
  2324. #endif
  2325. .lock = ocfs2_lock,
  2326. .flock = ocfs2_flock,
  2327. };
  2328. /*
  2329. * POSIX-lockless variants of our file_operations.
  2330. *
  2331. * These will be used if the underlying cluster stack does not support
  2332. * posix file locking, if the user passes the "localflocks" mount
  2333. * option, or if we have a local-only fs.
  2334. *
  2335. * ocfs2_flock is in here because all stacks handle UNIX file locks,
  2336. * so we still want it in the case of no stack support for
  2337. * plocks. Internally, it will do the right thing when asked to ignore
  2338. * the cluster.
  2339. */
  2340. const struct file_operations ocfs2_fops_no_plocks = {
  2341. .llseek = ocfs2_file_llseek,
  2342. .read = do_sync_read,
  2343. .write = do_sync_write,
  2344. .mmap = ocfs2_mmap,
  2345. .fsync = ocfs2_sync_file,
  2346. .release = ocfs2_file_release,
  2347. .open = ocfs2_file_open,
  2348. .aio_read = ocfs2_file_aio_read,
  2349. .aio_write = ocfs2_file_aio_write,
  2350. .unlocked_ioctl = ocfs2_ioctl,
  2351. #ifdef CONFIG_COMPAT
  2352. .compat_ioctl = ocfs2_compat_ioctl,
  2353. #endif
  2354. .flock = ocfs2_flock,
  2355. .splice_read = ocfs2_file_splice_read,
  2356. .splice_write = ocfs2_file_splice_write,
  2357. .fallocate = ocfs2_fallocate,
  2358. };
  2359. const struct file_operations ocfs2_dops_no_plocks = {
  2360. .llseek = generic_file_llseek,
  2361. .read = generic_read_dir,
  2362. .iterate = ocfs2_readdir,
  2363. .fsync = ocfs2_sync_file,
  2364. .release = ocfs2_dir_release,
  2365. .open = ocfs2_dir_open,
  2366. .unlocked_ioctl = ocfs2_ioctl,
  2367. #ifdef CONFIG_COMPAT
  2368. .compat_ioctl = ocfs2_compat_ioctl,
  2369. #endif
  2370. .flock = ocfs2_flock,
  2371. };