file.c 62 KB

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