file.c 65 KB

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