file.c 66 KB

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