file.c 65 KB

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