file.c 64 KB

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