file.c 71 KB

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