file.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178
  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. #define MLOG_MASK_PREFIX ML_INODE
  38. #include <cluster/masklog.h>
  39. #include "ocfs2.h"
  40. #include "alloc.h"
  41. #include "aops.h"
  42. #include "dir.h"
  43. #include "dlmglue.h"
  44. #include "extent_map.h"
  45. #include "file.h"
  46. #include "sysfile.h"
  47. #include "inode.h"
  48. #include "ioctl.h"
  49. #include "journal.h"
  50. #include "locks.h"
  51. #include "mmap.h"
  52. #include "suballoc.h"
  53. #include "super.h"
  54. #include "xattr.h"
  55. #include "buffer_head_io.h"
  56. static int ocfs2_sync_inode(struct inode *inode)
  57. {
  58. filemap_fdatawrite(inode->i_mapping);
  59. return sync_mapping_buffers(inode->i_mapping);
  60. }
  61. static int ocfs2_init_file_private(struct inode *inode, struct file *file)
  62. {
  63. struct ocfs2_file_private *fp;
  64. fp = kzalloc(sizeof(struct ocfs2_file_private), GFP_KERNEL);
  65. if (!fp)
  66. return -ENOMEM;
  67. fp->fp_file = file;
  68. mutex_init(&fp->fp_mutex);
  69. ocfs2_file_lock_res_init(&fp->fp_flock, fp);
  70. file->private_data = fp;
  71. return 0;
  72. }
  73. static void ocfs2_free_file_private(struct inode *inode, struct file *file)
  74. {
  75. struct ocfs2_file_private *fp = file->private_data;
  76. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  77. if (fp) {
  78. ocfs2_simple_drop_lockres(osb, &fp->fp_flock);
  79. ocfs2_lock_res_free(&fp->fp_flock);
  80. kfree(fp);
  81. file->private_data = NULL;
  82. }
  83. }
  84. static int ocfs2_file_open(struct inode *inode, struct file *file)
  85. {
  86. int status;
  87. int mode = file->f_flags;
  88. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  89. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  90. file->f_path.dentry->d_name.len, file->f_path.dentry->d_name.name);
  91. spin_lock(&oi->ip_lock);
  92. /* Check that the inode hasn't been wiped from disk by another
  93. * node. If it hasn't then we're safe as long as we hold the
  94. * spin lock until our increment of open count. */
  95. if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) {
  96. spin_unlock(&oi->ip_lock);
  97. status = -ENOENT;
  98. goto leave;
  99. }
  100. if (mode & O_DIRECT)
  101. oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT;
  102. oi->ip_open_count++;
  103. spin_unlock(&oi->ip_lock);
  104. status = ocfs2_init_file_private(inode, file);
  105. if (status) {
  106. /*
  107. * We want to set open count back if we're failing the
  108. * open.
  109. */
  110. spin_lock(&oi->ip_lock);
  111. oi->ip_open_count--;
  112. spin_unlock(&oi->ip_lock);
  113. }
  114. leave:
  115. mlog_exit(status);
  116. return status;
  117. }
  118. static int ocfs2_file_release(struct inode *inode, struct file *file)
  119. {
  120. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  121. mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file,
  122. file->f_path.dentry->d_name.len,
  123. file->f_path.dentry->d_name.name);
  124. spin_lock(&oi->ip_lock);
  125. if (!--oi->ip_open_count)
  126. oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT;
  127. spin_unlock(&oi->ip_lock);
  128. ocfs2_free_file_private(inode, file);
  129. mlog_exit(0);
  130. return 0;
  131. }
  132. static int ocfs2_dir_open(struct inode *inode, struct file *file)
  133. {
  134. return ocfs2_init_file_private(inode, file);
  135. }
  136. static int ocfs2_dir_release(struct inode *inode, struct file *file)
  137. {
  138. ocfs2_free_file_private(inode, file);
  139. return 0;
  140. }
  141. static int ocfs2_sync_file(struct file *file,
  142. struct dentry *dentry,
  143. int datasync)
  144. {
  145. int err = 0;
  146. journal_t *journal;
  147. struct inode *inode = dentry->d_inode;
  148. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  149. mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync,
  150. dentry->d_name.len, dentry->d_name.name);
  151. err = ocfs2_sync_inode(dentry->d_inode);
  152. if (err)
  153. goto bail;
  154. journal = osb->journal->j_journal;
  155. err = journal_force_commit(journal);
  156. bail:
  157. mlog_exit(err);
  158. return (err < 0) ? -EIO : 0;
  159. }
  160. int ocfs2_should_update_atime(struct inode *inode,
  161. struct vfsmount *vfsmnt)
  162. {
  163. struct timespec now;
  164. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  165. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  166. return 0;
  167. if ((inode->i_flags & S_NOATIME) ||
  168. ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode)))
  169. return 0;
  170. /*
  171. * We can be called with no vfsmnt structure - NFSD will
  172. * sometimes do this.
  173. *
  174. * Note that our action here is different than touch_atime() -
  175. * if we can't tell whether this is a noatime mount, then we
  176. * don't know whether to trust the value of s_atime_quantum.
  177. */
  178. if (vfsmnt == NULL)
  179. return 0;
  180. if ((vfsmnt->mnt_flags & MNT_NOATIME) ||
  181. ((vfsmnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  182. return 0;
  183. if (vfsmnt->mnt_flags & MNT_RELATIME) {
  184. if ((timespec_compare(&inode->i_atime, &inode->i_mtime) <= 0) ||
  185. (timespec_compare(&inode->i_atime, &inode->i_ctime) <= 0))
  186. return 1;
  187. return 0;
  188. }
  189. now = CURRENT_TIME;
  190. if ((now.tv_sec - inode->i_atime.tv_sec <= osb->s_atime_quantum))
  191. return 0;
  192. else
  193. return 1;
  194. }
  195. int ocfs2_update_inode_atime(struct inode *inode,
  196. struct buffer_head *bh)
  197. {
  198. int ret;
  199. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  200. handle_t *handle;
  201. struct ocfs2_dinode *di = (struct ocfs2_dinode *) bh->b_data;
  202. mlog_entry_void();
  203. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  204. if (handle == NULL) {
  205. ret = -ENOMEM;
  206. mlog_errno(ret);
  207. goto out;
  208. }
  209. ret = ocfs2_journal_access(handle, inode, bh,
  210. OCFS2_JOURNAL_ACCESS_WRITE);
  211. if (ret) {
  212. mlog_errno(ret);
  213. goto out_commit;
  214. }
  215. /*
  216. * Don't use ocfs2_mark_inode_dirty() here as we don't always
  217. * have i_mutex to guard against concurrent changes to other
  218. * inode fields.
  219. */
  220. inode->i_atime = CURRENT_TIME;
  221. di->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
  222. di->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
  223. ret = ocfs2_journal_dirty(handle, bh);
  224. if (ret < 0)
  225. mlog_errno(ret);
  226. out_commit:
  227. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  228. out:
  229. mlog_exit(ret);
  230. return ret;
  231. }
  232. static int ocfs2_set_inode_size(handle_t *handle,
  233. struct inode *inode,
  234. struct buffer_head *fe_bh,
  235. u64 new_i_size)
  236. {
  237. int status;
  238. mlog_entry_void();
  239. i_size_write(inode, new_i_size);
  240. inode->i_blocks = ocfs2_inode_sector_count(inode);
  241. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  242. status = ocfs2_mark_inode_dirty(handle, inode, fe_bh);
  243. if (status < 0) {
  244. mlog_errno(status);
  245. goto bail;
  246. }
  247. bail:
  248. mlog_exit(status);
  249. return status;
  250. }
  251. static int ocfs2_simple_size_update(struct inode *inode,
  252. struct buffer_head *di_bh,
  253. u64 new_i_size)
  254. {
  255. int ret;
  256. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  257. handle_t *handle = NULL;
  258. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  259. if (handle == NULL) {
  260. ret = -ENOMEM;
  261. mlog_errno(ret);
  262. goto out;
  263. }
  264. ret = ocfs2_set_inode_size(handle, inode, di_bh,
  265. new_i_size);
  266. if (ret < 0)
  267. mlog_errno(ret);
  268. ocfs2_commit_trans(osb, handle);
  269. out:
  270. return ret;
  271. }
  272. static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb,
  273. struct inode *inode,
  274. struct buffer_head *fe_bh,
  275. u64 new_i_size)
  276. {
  277. int status;
  278. handle_t *handle;
  279. struct ocfs2_dinode *di;
  280. u64 cluster_bytes;
  281. mlog_entry_void();
  282. /* TODO: This needs to actually orphan the inode in this
  283. * transaction. */
  284. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  285. if (IS_ERR(handle)) {
  286. status = PTR_ERR(handle);
  287. mlog_errno(status);
  288. goto out;
  289. }
  290. status = ocfs2_journal_access(handle, inode, fe_bh,
  291. OCFS2_JOURNAL_ACCESS_WRITE);
  292. if (status < 0) {
  293. mlog_errno(status);
  294. goto out_commit;
  295. }
  296. /*
  297. * Do this before setting i_size.
  298. */
  299. cluster_bytes = ocfs2_align_bytes_to_clusters(inode->i_sb, new_i_size);
  300. status = ocfs2_zero_range_for_truncate(inode, handle, new_i_size,
  301. cluster_bytes);
  302. if (status) {
  303. mlog_errno(status);
  304. goto out_commit;
  305. }
  306. i_size_write(inode, new_i_size);
  307. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  308. di = (struct ocfs2_dinode *) fe_bh->b_data;
  309. di->i_size = cpu_to_le64(new_i_size);
  310. di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
  311. di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  312. status = ocfs2_journal_dirty(handle, fe_bh);
  313. if (status < 0)
  314. mlog_errno(status);
  315. out_commit:
  316. ocfs2_commit_trans(osb, handle);
  317. out:
  318. mlog_exit(status);
  319. return status;
  320. }
  321. static int ocfs2_truncate_file(struct inode *inode,
  322. struct buffer_head *di_bh,
  323. u64 new_i_size)
  324. {
  325. int status = 0;
  326. struct ocfs2_dinode *fe = NULL;
  327. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  328. struct ocfs2_truncate_context *tc = NULL;
  329. mlog_entry("(inode = %llu, new_i_size = %llu\n",
  330. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  331. (unsigned long long)new_i_size);
  332. fe = (struct ocfs2_dinode *) di_bh->b_data;
  333. if (!OCFS2_IS_VALID_DINODE(fe)) {
  334. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  335. status = -EIO;
  336. goto bail;
  337. }
  338. mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode),
  339. "Inode %llu, inode i_size = %lld != di "
  340. "i_size = %llu, i_flags = 0x%x\n",
  341. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  342. i_size_read(inode),
  343. (unsigned long long)le64_to_cpu(fe->i_size),
  344. le32_to_cpu(fe->i_flags));
  345. if (new_i_size > le64_to_cpu(fe->i_size)) {
  346. mlog(0, "asked to truncate file with size (%llu) to size (%llu)!\n",
  347. (unsigned long long)le64_to_cpu(fe->i_size),
  348. (unsigned long long)new_i_size);
  349. status = -EINVAL;
  350. mlog_errno(status);
  351. goto bail;
  352. }
  353. mlog(0, "inode %llu, i_size = %llu, new_i_size = %llu\n",
  354. (unsigned long long)le64_to_cpu(fe->i_blkno),
  355. (unsigned long long)le64_to_cpu(fe->i_size),
  356. (unsigned long long)new_i_size);
  357. /* lets handle the simple truncate cases before doing any more
  358. * cluster locking. */
  359. if (new_i_size == le64_to_cpu(fe->i_size))
  360. goto bail;
  361. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  362. /*
  363. * The inode lock forced other nodes to sync and drop their
  364. * pages, which (correctly) happens even if we have a truncate
  365. * without allocation change - ocfs2 cluster sizes can be much
  366. * greater than page size, so we have to truncate them
  367. * anyway.
  368. */
  369. unmap_mapping_range(inode->i_mapping, new_i_size + PAGE_SIZE - 1, 0, 1);
  370. truncate_inode_pages(inode->i_mapping, new_i_size);
  371. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  372. status = ocfs2_truncate_inline(inode, di_bh, new_i_size,
  373. i_size_read(inode), 1);
  374. if (status)
  375. mlog_errno(status);
  376. goto bail_unlock_sem;
  377. }
  378. /* alright, we're going to need to do a full blown alloc size
  379. * change. Orphan the inode so that recovery can complete the
  380. * truncate if necessary. This does the task of marking
  381. * i_size. */
  382. status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size);
  383. if (status < 0) {
  384. mlog_errno(status);
  385. goto bail_unlock_sem;
  386. }
  387. status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc);
  388. if (status < 0) {
  389. mlog_errno(status);
  390. goto bail_unlock_sem;
  391. }
  392. status = ocfs2_commit_truncate(osb, inode, di_bh, tc);
  393. if (status < 0) {
  394. mlog_errno(status);
  395. goto bail_unlock_sem;
  396. }
  397. /* TODO: orphan dir cleanup here. */
  398. bail_unlock_sem:
  399. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  400. bail:
  401. mlog_exit(status);
  402. return status;
  403. }
  404. /*
  405. * extend file allocation only here.
  406. * we'll update all the disk stuff, and oip->alloc_size
  407. *
  408. * expect stuff to be locked, a transaction started and enough data /
  409. * metadata reservations in the contexts.
  410. *
  411. * Will return -EAGAIN, and a reason if a restart is needed.
  412. * If passed in, *reason will always be set, even in error.
  413. */
  414. int ocfs2_add_inode_data(struct ocfs2_super *osb,
  415. struct inode *inode,
  416. u32 *logical_offset,
  417. u32 clusters_to_add,
  418. int mark_unwritten,
  419. struct buffer_head *fe_bh,
  420. handle_t *handle,
  421. struct ocfs2_alloc_context *data_ac,
  422. struct ocfs2_alloc_context *meta_ac,
  423. enum ocfs2_alloc_restarted *reason_ret)
  424. {
  425. int ret;
  426. struct ocfs2_extent_tree et;
  427. ocfs2_get_dinode_extent_tree(&et, inode, fe_bh);
  428. ret = ocfs2_add_clusters_in_btree(osb, inode, logical_offset,
  429. clusters_to_add, mark_unwritten,
  430. &et, handle,
  431. data_ac, meta_ac, reason_ret);
  432. ocfs2_put_extent_tree(&et);
  433. return ret;
  434. }
  435. static int __ocfs2_extend_allocation(struct inode *inode, u32 logical_start,
  436. u32 clusters_to_add, int mark_unwritten)
  437. {
  438. int status = 0;
  439. int restart_func = 0;
  440. int credits;
  441. u32 prev_clusters;
  442. struct buffer_head *bh = NULL;
  443. struct ocfs2_dinode *fe = NULL;
  444. handle_t *handle = NULL;
  445. struct ocfs2_alloc_context *data_ac = NULL;
  446. struct ocfs2_alloc_context *meta_ac = NULL;
  447. enum ocfs2_alloc_restarted why;
  448. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  449. struct ocfs2_extent_tree et;
  450. mlog_entry("(clusters_to_add = %u)\n", clusters_to_add);
  451. /*
  452. * This function only exists for file systems which don't
  453. * support holes.
  454. */
  455. BUG_ON(mark_unwritten && !ocfs2_sparse_alloc(osb));
  456. status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
  457. OCFS2_BH_CACHED, inode);
  458. if (status < 0) {
  459. mlog_errno(status);
  460. goto leave;
  461. }
  462. fe = (struct ocfs2_dinode *) bh->b_data;
  463. if (!OCFS2_IS_VALID_DINODE(fe)) {
  464. OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
  465. status = -EIO;
  466. goto leave;
  467. }
  468. restart_all:
  469. BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters);
  470. mlog(0, "extend inode %llu, i_size = %lld, di->i_clusters = %u, "
  471. "clusters_to_add = %u\n",
  472. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  473. (long long)i_size_read(inode), le32_to_cpu(fe->i_clusters),
  474. clusters_to_add);
  475. ocfs2_get_dinode_extent_tree(&et, inode, bh);
  476. status = ocfs2_lock_allocators(inode, &et, clusters_to_add, 0,
  477. &data_ac, &meta_ac);
  478. ocfs2_put_extent_tree(&et);
  479. if (status) {
  480. mlog_errno(status);
  481. goto leave;
  482. }
  483. credits = ocfs2_calc_extend_credits(osb->sb, &fe->id2.i_list,
  484. clusters_to_add);
  485. handle = ocfs2_start_trans(osb, credits);
  486. if (IS_ERR(handle)) {
  487. status = PTR_ERR(handle);
  488. handle = NULL;
  489. mlog_errno(status);
  490. goto leave;
  491. }
  492. restarted_transaction:
  493. /* reserve a write to the file entry early on - that we if we
  494. * run out of credits in the allocation path, we can still
  495. * update i_size. */
  496. status = ocfs2_journal_access(handle, inode, bh,
  497. OCFS2_JOURNAL_ACCESS_WRITE);
  498. if (status < 0) {
  499. mlog_errno(status);
  500. goto leave;
  501. }
  502. prev_clusters = OCFS2_I(inode)->ip_clusters;
  503. status = ocfs2_add_inode_data(osb,
  504. inode,
  505. &logical_start,
  506. clusters_to_add,
  507. mark_unwritten,
  508. bh,
  509. handle,
  510. data_ac,
  511. meta_ac,
  512. &why);
  513. if ((status < 0) && (status != -EAGAIN)) {
  514. if (status != -ENOSPC)
  515. mlog_errno(status);
  516. goto leave;
  517. }
  518. status = ocfs2_journal_dirty(handle, bh);
  519. if (status < 0) {
  520. mlog_errno(status);
  521. goto leave;
  522. }
  523. spin_lock(&OCFS2_I(inode)->ip_lock);
  524. clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters);
  525. spin_unlock(&OCFS2_I(inode)->ip_lock);
  526. if (why != RESTART_NONE && clusters_to_add) {
  527. if (why == RESTART_META) {
  528. mlog(0, "restarting function.\n");
  529. restart_func = 1;
  530. } else {
  531. BUG_ON(why != RESTART_TRANS);
  532. mlog(0, "restarting transaction.\n");
  533. /* TODO: This can be more intelligent. */
  534. credits = ocfs2_calc_extend_credits(osb->sb,
  535. &fe->id2.i_list,
  536. clusters_to_add);
  537. status = ocfs2_extend_trans(handle, credits);
  538. if (status < 0) {
  539. /* handle still has to be committed at
  540. * this point. */
  541. status = -ENOMEM;
  542. mlog_errno(status);
  543. goto leave;
  544. }
  545. goto restarted_transaction;
  546. }
  547. }
  548. mlog(0, "fe: i_clusters = %u, i_size=%llu\n",
  549. le32_to_cpu(fe->i_clusters),
  550. (unsigned long long)le64_to_cpu(fe->i_size));
  551. mlog(0, "inode: ip_clusters=%u, i_size=%lld\n",
  552. OCFS2_I(inode)->ip_clusters, (long long)i_size_read(inode));
  553. leave:
  554. if (handle) {
  555. ocfs2_commit_trans(osb, handle);
  556. handle = NULL;
  557. }
  558. if (data_ac) {
  559. ocfs2_free_alloc_context(data_ac);
  560. data_ac = NULL;
  561. }
  562. if (meta_ac) {
  563. ocfs2_free_alloc_context(meta_ac);
  564. meta_ac = NULL;
  565. }
  566. if ((!status) && restart_func) {
  567. restart_func = 0;
  568. goto restart_all;
  569. }
  570. if (bh) {
  571. brelse(bh);
  572. bh = NULL;
  573. }
  574. mlog_exit(status);
  575. return status;
  576. }
  577. /* Some parts of this taken from generic_cont_expand, which turned out
  578. * to be too fragile to do exactly what we need without us having to
  579. * worry about recursive locking in ->prepare_write() and
  580. * ->commit_write(). */
  581. static int ocfs2_write_zero_page(struct inode *inode,
  582. u64 size)
  583. {
  584. struct address_space *mapping = inode->i_mapping;
  585. struct page *page;
  586. unsigned long index;
  587. unsigned int offset;
  588. handle_t *handle = NULL;
  589. int ret;
  590. offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */
  591. /* ugh. in prepare/commit_write, if from==to==start of block, we
  592. ** skip the prepare. make sure we never send an offset for the start
  593. ** of a block
  594. */
  595. if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) {
  596. offset++;
  597. }
  598. index = size >> PAGE_CACHE_SHIFT;
  599. page = grab_cache_page(mapping, index);
  600. if (!page) {
  601. ret = -ENOMEM;
  602. mlog_errno(ret);
  603. goto out;
  604. }
  605. ret = ocfs2_prepare_write_nolock(inode, page, offset, offset);
  606. if (ret < 0) {
  607. mlog_errno(ret);
  608. goto out_unlock;
  609. }
  610. if (ocfs2_should_order_data(inode)) {
  611. handle = ocfs2_start_walk_page_trans(inode, page, offset,
  612. offset);
  613. if (IS_ERR(handle)) {
  614. ret = PTR_ERR(handle);
  615. handle = NULL;
  616. goto out_unlock;
  617. }
  618. }
  619. /* must not update i_size! */
  620. ret = block_commit_write(page, offset, offset);
  621. if (ret < 0)
  622. mlog_errno(ret);
  623. else
  624. ret = 0;
  625. if (handle)
  626. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  627. out_unlock:
  628. unlock_page(page);
  629. page_cache_release(page);
  630. out:
  631. return ret;
  632. }
  633. static int ocfs2_zero_extend(struct inode *inode,
  634. u64 zero_to_size)
  635. {
  636. int ret = 0;
  637. u64 start_off;
  638. struct super_block *sb = inode->i_sb;
  639. start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode));
  640. while (start_off < zero_to_size) {
  641. ret = ocfs2_write_zero_page(inode, start_off);
  642. if (ret < 0) {
  643. mlog_errno(ret);
  644. goto out;
  645. }
  646. start_off += sb->s_blocksize;
  647. /*
  648. * Very large extends have the potential to lock up
  649. * the cpu for extended periods of time.
  650. */
  651. cond_resched();
  652. }
  653. out:
  654. return ret;
  655. }
  656. int ocfs2_extend_no_holes(struct inode *inode, u64 new_i_size, u64 zero_to)
  657. {
  658. int ret;
  659. u32 clusters_to_add;
  660. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  661. clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size);
  662. if (clusters_to_add < oi->ip_clusters)
  663. clusters_to_add = 0;
  664. else
  665. clusters_to_add -= oi->ip_clusters;
  666. if (clusters_to_add) {
  667. ret = __ocfs2_extend_allocation(inode, oi->ip_clusters,
  668. clusters_to_add, 0);
  669. if (ret) {
  670. mlog_errno(ret);
  671. goto out;
  672. }
  673. }
  674. /*
  675. * Call this even if we don't add any clusters to the tree. We
  676. * still need to zero the area between the old i_size and the
  677. * new i_size.
  678. */
  679. ret = ocfs2_zero_extend(inode, zero_to);
  680. if (ret < 0)
  681. mlog_errno(ret);
  682. out:
  683. return ret;
  684. }
  685. static int ocfs2_extend_file(struct inode *inode,
  686. struct buffer_head *di_bh,
  687. u64 new_i_size)
  688. {
  689. int ret = 0;
  690. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  691. BUG_ON(!di_bh);
  692. /* setattr sometimes calls us like this. */
  693. if (new_i_size == 0)
  694. goto out;
  695. if (i_size_read(inode) == new_i_size)
  696. goto out;
  697. BUG_ON(new_i_size < i_size_read(inode));
  698. /*
  699. * Fall through for converting inline data, even if the fs
  700. * supports sparse files.
  701. *
  702. * The check for inline data here is legal - nobody can add
  703. * the feature since we have i_mutex. We must check it again
  704. * after acquiring ip_alloc_sem though, as paths like mmap
  705. * might have raced us to converting the inode to extents.
  706. */
  707. if (!(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
  708. && ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  709. goto out_update_size;
  710. /*
  711. * The alloc sem blocks people in read/write from reading our
  712. * allocation until we're done changing it. We depend on
  713. * i_mutex to block other extend/truncate calls while we're
  714. * here.
  715. */
  716. down_write(&oi->ip_alloc_sem);
  717. if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  718. /*
  719. * We can optimize small extends by keeping the inodes
  720. * inline data.
  721. */
  722. if (ocfs2_size_fits_inline_data(di_bh, new_i_size)) {
  723. up_write(&oi->ip_alloc_sem);
  724. goto out_update_size;
  725. }
  726. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  727. if (ret) {
  728. up_write(&oi->ip_alloc_sem);
  729. mlog_errno(ret);
  730. goto out;
  731. }
  732. }
  733. if (!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
  734. ret = ocfs2_extend_no_holes(inode, new_i_size, new_i_size);
  735. up_write(&oi->ip_alloc_sem);
  736. if (ret < 0) {
  737. mlog_errno(ret);
  738. goto out;
  739. }
  740. out_update_size:
  741. ret = ocfs2_simple_size_update(inode, di_bh, new_i_size);
  742. if (ret < 0)
  743. mlog_errno(ret);
  744. out:
  745. return ret;
  746. }
  747. int ocfs2_setattr(struct dentry *dentry, struct iattr *attr)
  748. {
  749. int status = 0, size_change;
  750. struct inode *inode = dentry->d_inode;
  751. struct super_block *sb = inode->i_sb;
  752. struct ocfs2_super *osb = OCFS2_SB(sb);
  753. struct buffer_head *bh = NULL;
  754. handle_t *handle = NULL;
  755. mlog_entry("(0x%p, '%.*s')\n", dentry,
  756. dentry->d_name.len, dentry->d_name.name);
  757. /* ensuring we don't even attempt to truncate a symlink */
  758. if (S_ISLNK(inode->i_mode))
  759. attr->ia_valid &= ~ATTR_SIZE;
  760. if (attr->ia_valid & ATTR_MODE)
  761. mlog(0, "mode change: %d\n", attr->ia_mode);
  762. if (attr->ia_valid & ATTR_UID)
  763. mlog(0, "uid change: %d\n", attr->ia_uid);
  764. if (attr->ia_valid & ATTR_GID)
  765. mlog(0, "gid change: %d\n", attr->ia_gid);
  766. if (attr->ia_valid & ATTR_SIZE)
  767. mlog(0, "size change...\n");
  768. if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME))
  769. mlog(0, "time change...\n");
  770. #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \
  771. | ATTR_GID | ATTR_UID | ATTR_MODE)
  772. if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) {
  773. mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid);
  774. return 0;
  775. }
  776. status = inode_change_ok(inode, attr);
  777. if (status)
  778. return status;
  779. size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE;
  780. if (size_change) {
  781. status = ocfs2_rw_lock(inode, 1);
  782. if (status < 0) {
  783. mlog_errno(status);
  784. goto bail;
  785. }
  786. }
  787. status = ocfs2_inode_lock(inode, &bh, 1);
  788. if (status < 0) {
  789. if (status != -ENOENT)
  790. mlog_errno(status);
  791. goto bail_unlock_rw;
  792. }
  793. if (size_change && attr->ia_size != i_size_read(inode)) {
  794. if (attr->ia_size > sb->s_maxbytes) {
  795. status = -EFBIG;
  796. goto bail_unlock;
  797. }
  798. if (i_size_read(inode) > attr->ia_size)
  799. status = ocfs2_truncate_file(inode, bh, attr->ia_size);
  800. else
  801. status = ocfs2_extend_file(inode, bh, attr->ia_size);
  802. if (status < 0) {
  803. if (status != -ENOSPC)
  804. mlog_errno(status);
  805. status = -ENOSPC;
  806. goto bail_unlock;
  807. }
  808. }
  809. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  810. if (IS_ERR(handle)) {
  811. status = PTR_ERR(handle);
  812. mlog_errno(status);
  813. goto bail_unlock;
  814. }
  815. /*
  816. * This will intentionally not wind up calling vmtruncate(),
  817. * since all the work for a size change has been done above.
  818. * Otherwise, we could get into problems with truncate as
  819. * ip_alloc_sem is used there to protect against i_size
  820. * changes.
  821. */
  822. status = inode_setattr(inode, attr);
  823. if (status < 0) {
  824. mlog_errno(status);
  825. goto bail_commit;
  826. }
  827. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  828. if (status < 0)
  829. mlog_errno(status);
  830. bail_commit:
  831. ocfs2_commit_trans(osb, handle);
  832. bail_unlock:
  833. ocfs2_inode_unlock(inode, 1);
  834. bail_unlock_rw:
  835. if (size_change)
  836. ocfs2_rw_unlock(inode, 1);
  837. bail:
  838. if (bh)
  839. brelse(bh);
  840. mlog_exit(status);
  841. return status;
  842. }
  843. int ocfs2_getattr(struct vfsmount *mnt,
  844. struct dentry *dentry,
  845. struct kstat *stat)
  846. {
  847. struct inode *inode = dentry->d_inode;
  848. struct super_block *sb = dentry->d_inode->i_sb;
  849. struct ocfs2_super *osb = sb->s_fs_info;
  850. int err;
  851. mlog_entry_void();
  852. err = ocfs2_inode_revalidate(dentry);
  853. if (err) {
  854. if (err != -ENOENT)
  855. mlog_errno(err);
  856. goto bail;
  857. }
  858. generic_fillattr(inode, stat);
  859. /* We set the blksize from the cluster size for performance */
  860. stat->blksize = osb->s_clustersize;
  861. bail:
  862. mlog_exit(err);
  863. return err;
  864. }
  865. int ocfs2_permission(struct inode *inode, int mask)
  866. {
  867. int ret;
  868. mlog_entry_void();
  869. ret = ocfs2_inode_lock(inode, NULL, 0);
  870. if (ret) {
  871. if (ret != -ENOENT)
  872. mlog_errno(ret);
  873. goto out;
  874. }
  875. ret = generic_permission(inode, mask, NULL);
  876. ocfs2_inode_unlock(inode, 0);
  877. out:
  878. mlog_exit(ret);
  879. return ret;
  880. }
  881. static int __ocfs2_write_remove_suid(struct inode *inode,
  882. struct buffer_head *bh)
  883. {
  884. int ret;
  885. handle_t *handle;
  886. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  887. struct ocfs2_dinode *di;
  888. mlog_entry("(Inode %llu, mode 0%o)\n",
  889. (unsigned long long)OCFS2_I(inode)->ip_blkno, inode->i_mode);
  890. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  891. if (handle == NULL) {
  892. ret = -ENOMEM;
  893. mlog_errno(ret);
  894. goto out;
  895. }
  896. ret = ocfs2_journal_access(handle, inode, bh,
  897. OCFS2_JOURNAL_ACCESS_WRITE);
  898. if (ret < 0) {
  899. mlog_errno(ret);
  900. goto out_trans;
  901. }
  902. inode->i_mode &= ~S_ISUID;
  903. if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP))
  904. inode->i_mode &= ~S_ISGID;
  905. di = (struct ocfs2_dinode *) bh->b_data;
  906. di->i_mode = cpu_to_le16(inode->i_mode);
  907. ret = ocfs2_journal_dirty(handle, bh);
  908. if (ret < 0)
  909. mlog_errno(ret);
  910. out_trans:
  911. ocfs2_commit_trans(osb, handle);
  912. out:
  913. mlog_exit(ret);
  914. return ret;
  915. }
  916. /*
  917. * Will look for holes and unwritten extents in the range starting at
  918. * pos for count bytes (inclusive).
  919. */
  920. static int ocfs2_check_range_for_holes(struct inode *inode, loff_t pos,
  921. size_t count)
  922. {
  923. int ret = 0;
  924. unsigned int extent_flags;
  925. u32 cpos, clusters, extent_len, phys_cpos;
  926. struct super_block *sb = inode->i_sb;
  927. cpos = pos >> OCFS2_SB(sb)->s_clustersize_bits;
  928. clusters = ocfs2_clusters_for_bytes(sb, pos + count) - cpos;
  929. while (clusters) {
  930. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &extent_len,
  931. &extent_flags);
  932. if (ret < 0) {
  933. mlog_errno(ret);
  934. goto out;
  935. }
  936. if (phys_cpos == 0 || (extent_flags & OCFS2_EXT_UNWRITTEN)) {
  937. ret = 1;
  938. break;
  939. }
  940. if (extent_len > clusters)
  941. extent_len = clusters;
  942. clusters -= extent_len;
  943. cpos += extent_len;
  944. }
  945. out:
  946. return ret;
  947. }
  948. static int ocfs2_write_remove_suid(struct inode *inode)
  949. {
  950. int ret;
  951. struct buffer_head *bh = NULL;
  952. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  953. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  954. oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode);
  955. if (ret < 0) {
  956. mlog_errno(ret);
  957. goto out;
  958. }
  959. ret = __ocfs2_write_remove_suid(inode, bh);
  960. out:
  961. brelse(bh);
  962. return ret;
  963. }
  964. /*
  965. * Allocate enough extents to cover the region starting at byte offset
  966. * start for len bytes. Existing extents are skipped, any extents
  967. * added are marked as "unwritten".
  968. */
  969. static int ocfs2_allocate_unwritten_extents(struct inode *inode,
  970. u64 start, u64 len)
  971. {
  972. int ret;
  973. u32 cpos, phys_cpos, clusters, alloc_size;
  974. u64 end = start + len;
  975. struct buffer_head *di_bh = NULL;
  976. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  977. ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
  978. OCFS2_I(inode)->ip_blkno, &di_bh,
  979. OCFS2_BH_CACHED, inode);
  980. if (ret) {
  981. mlog_errno(ret);
  982. goto out;
  983. }
  984. /*
  985. * Nothing to do if the requested reservation range
  986. * fits within the inode.
  987. */
  988. if (ocfs2_size_fits_inline_data(di_bh, end))
  989. goto out;
  990. ret = ocfs2_convert_inline_data_to_extents(inode, di_bh);
  991. if (ret) {
  992. mlog_errno(ret);
  993. goto out;
  994. }
  995. }
  996. /*
  997. * We consider both start and len to be inclusive.
  998. */
  999. cpos = start >> OCFS2_SB(inode->i_sb)->s_clustersize_bits;
  1000. clusters = ocfs2_clusters_for_bytes(inode->i_sb, start + len);
  1001. clusters -= cpos;
  1002. while (clusters) {
  1003. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1004. &alloc_size, NULL);
  1005. if (ret) {
  1006. mlog_errno(ret);
  1007. goto out;
  1008. }
  1009. /*
  1010. * Hole or existing extent len can be arbitrary, so
  1011. * cap it to our own allocation request.
  1012. */
  1013. if (alloc_size > clusters)
  1014. alloc_size = clusters;
  1015. if (phys_cpos) {
  1016. /*
  1017. * We already have an allocation at this
  1018. * region so we can safely skip it.
  1019. */
  1020. goto next;
  1021. }
  1022. ret = __ocfs2_extend_allocation(inode, cpos, alloc_size, 1);
  1023. if (ret) {
  1024. if (ret != -ENOSPC)
  1025. mlog_errno(ret);
  1026. goto out;
  1027. }
  1028. next:
  1029. cpos += alloc_size;
  1030. clusters -= alloc_size;
  1031. }
  1032. ret = 0;
  1033. out:
  1034. brelse(di_bh);
  1035. return ret;
  1036. }
  1037. static int __ocfs2_remove_inode_range(struct inode *inode,
  1038. struct buffer_head *di_bh,
  1039. u32 cpos, u32 phys_cpos, u32 len,
  1040. struct ocfs2_cached_dealloc_ctxt *dealloc)
  1041. {
  1042. int ret;
  1043. u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
  1044. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1045. struct inode *tl_inode = osb->osb_tl_inode;
  1046. handle_t *handle;
  1047. struct ocfs2_alloc_context *meta_ac = NULL;
  1048. struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
  1049. struct ocfs2_extent_tree et;
  1050. ocfs2_get_dinode_extent_tree(&et, inode, di_bh);
  1051. ret = ocfs2_lock_allocators(inode, &et, 0, 1, NULL, &meta_ac);
  1052. if (ret) {
  1053. ocfs2_put_extent_tree(&et);
  1054. mlog_errno(ret);
  1055. return ret;
  1056. }
  1057. mutex_lock(&tl_inode->i_mutex);
  1058. if (ocfs2_truncate_log_needs_flush(osb)) {
  1059. ret = __ocfs2_flush_truncate_log(osb);
  1060. if (ret < 0) {
  1061. mlog_errno(ret);
  1062. goto out;
  1063. }
  1064. }
  1065. handle = ocfs2_start_trans(osb, OCFS2_REMOVE_EXTENT_CREDITS);
  1066. if (handle == NULL) {
  1067. ret = -ENOMEM;
  1068. mlog_errno(ret);
  1069. goto out;
  1070. }
  1071. ret = ocfs2_journal_access(handle, inode, di_bh,
  1072. OCFS2_JOURNAL_ACCESS_WRITE);
  1073. if (ret) {
  1074. mlog_errno(ret);
  1075. goto out;
  1076. }
  1077. ret = ocfs2_remove_extent(inode, &et, cpos, len, handle, meta_ac,
  1078. dealloc);
  1079. if (ret) {
  1080. mlog_errno(ret);
  1081. goto out_commit;
  1082. }
  1083. OCFS2_I(inode)->ip_clusters -= len;
  1084. di->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters);
  1085. ret = ocfs2_journal_dirty(handle, di_bh);
  1086. if (ret) {
  1087. mlog_errno(ret);
  1088. goto out_commit;
  1089. }
  1090. ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
  1091. if (ret)
  1092. mlog_errno(ret);
  1093. out_commit:
  1094. ocfs2_commit_trans(osb, handle);
  1095. out:
  1096. mutex_unlock(&tl_inode->i_mutex);
  1097. if (meta_ac)
  1098. ocfs2_free_alloc_context(meta_ac);
  1099. ocfs2_put_extent_tree(&et);
  1100. return ret;
  1101. }
  1102. /*
  1103. * Truncate a byte range, avoiding pages within partial clusters. This
  1104. * preserves those pages for the zeroing code to write to.
  1105. */
  1106. static void ocfs2_truncate_cluster_pages(struct inode *inode, u64 byte_start,
  1107. u64 byte_len)
  1108. {
  1109. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1110. loff_t start, end;
  1111. struct address_space *mapping = inode->i_mapping;
  1112. start = (loff_t)ocfs2_align_bytes_to_clusters(inode->i_sb, byte_start);
  1113. end = byte_start + byte_len;
  1114. end = end & ~(osb->s_clustersize - 1);
  1115. if (start < end) {
  1116. unmap_mapping_range(mapping, start, end - start, 0);
  1117. truncate_inode_pages_range(mapping, start, end - 1);
  1118. }
  1119. }
  1120. static int ocfs2_zero_partial_clusters(struct inode *inode,
  1121. u64 start, u64 len)
  1122. {
  1123. int ret = 0;
  1124. u64 tmpend, end = start + len;
  1125. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1126. unsigned int csize = osb->s_clustersize;
  1127. handle_t *handle;
  1128. /*
  1129. * The "start" and "end" values are NOT necessarily part of
  1130. * the range whose allocation is being deleted. Rather, this
  1131. * is what the user passed in with the request. We must zero
  1132. * partial clusters here. There's no need to worry about
  1133. * physical allocation - the zeroing code knows to skip holes.
  1134. */
  1135. mlog(0, "byte start: %llu, end: %llu\n",
  1136. (unsigned long long)start, (unsigned long long)end);
  1137. /*
  1138. * If both edges are on a cluster boundary then there's no
  1139. * zeroing required as the region is part of the allocation to
  1140. * be truncated.
  1141. */
  1142. if ((start & (csize - 1)) == 0 && (end & (csize - 1)) == 0)
  1143. goto out;
  1144. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1145. if (handle == NULL) {
  1146. ret = -ENOMEM;
  1147. mlog_errno(ret);
  1148. goto out;
  1149. }
  1150. /*
  1151. * We want to get the byte offset of the end of the 1st cluster.
  1152. */
  1153. tmpend = (u64)osb->s_clustersize + (start & ~(osb->s_clustersize - 1));
  1154. if (tmpend > end)
  1155. tmpend = end;
  1156. mlog(0, "1st range: start: %llu, tmpend: %llu\n",
  1157. (unsigned long long)start, (unsigned long long)tmpend);
  1158. ret = ocfs2_zero_range_for_truncate(inode, handle, start, tmpend);
  1159. if (ret)
  1160. mlog_errno(ret);
  1161. if (tmpend < end) {
  1162. /*
  1163. * This may make start and end equal, but the zeroing
  1164. * code will skip any work in that case so there's no
  1165. * need to catch it up here.
  1166. */
  1167. start = end & ~(osb->s_clustersize - 1);
  1168. mlog(0, "2nd range: start: %llu, end: %llu\n",
  1169. (unsigned long long)start, (unsigned long long)end);
  1170. ret = ocfs2_zero_range_for_truncate(inode, handle, start, end);
  1171. if (ret)
  1172. mlog_errno(ret);
  1173. }
  1174. ocfs2_commit_trans(osb, handle);
  1175. out:
  1176. return ret;
  1177. }
  1178. static int ocfs2_remove_inode_range(struct inode *inode,
  1179. struct buffer_head *di_bh, u64 byte_start,
  1180. u64 byte_len)
  1181. {
  1182. int ret = 0;
  1183. u32 trunc_start, trunc_len, cpos, phys_cpos, alloc_size;
  1184. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1185. struct ocfs2_cached_dealloc_ctxt dealloc;
  1186. struct address_space *mapping = inode->i_mapping;
  1187. ocfs2_init_dealloc_ctxt(&dealloc);
  1188. if (byte_len == 0)
  1189. return 0;
  1190. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1191. ret = ocfs2_truncate_inline(inode, di_bh, byte_start,
  1192. byte_start + byte_len, 0);
  1193. if (ret) {
  1194. mlog_errno(ret);
  1195. goto out;
  1196. }
  1197. /*
  1198. * There's no need to get fancy with the page cache
  1199. * truncate of an inline-data inode. We're talking
  1200. * about less than a page here, which will be cached
  1201. * in the dinode buffer anyway.
  1202. */
  1203. unmap_mapping_range(mapping, 0, 0, 0);
  1204. truncate_inode_pages(mapping, 0);
  1205. goto out;
  1206. }
  1207. trunc_start = ocfs2_clusters_for_bytes(osb->sb, byte_start);
  1208. trunc_len = (byte_start + byte_len) >> osb->s_clustersize_bits;
  1209. if (trunc_len >= trunc_start)
  1210. trunc_len -= trunc_start;
  1211. else
  1212. trunc_len = 0;
  1213. mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u\n",
  1214. (unsigned long long)OCFS2_I(inode)->ip_blkno,
  1215. (unsigned long long)byte_start,
  1216. (unsigned long long)byte_len, trunc_start, trunc_len);
  1217. ret = ocfs2_zero_partial_clusters(inode, byte_start, byte_len);
  1218. if (ret) {
  1219. mlog_errno(ret);
  1220. goto out;
  1221. }
  1222. cpos = trunc_start;
  1223. while (trunc_len) {
  1224. ret = ocfs2_get_clusters(inode, cpos, &phys_cpos,
  1225. &alloc_size, NULL);
  1226. if (ret) {
  1227. mlog_errno(ret);
  1228. goto out;
  1229. }
  1230. if (alloc_size > trunc_len)
  1231. alloc_size = trunc_len;
  1232. /* Only do work for non-holes */
  1233. if (phys_cpos != 0) {
  1234. ret = __ocfs2_remove_inode_range(inode, di_bh, cpos,
  1235. phys_cpos, alloc_size,
  1236. &dealloc);
  1237. if (ret) {
  1238. mlog_errno(ret);
  1239. goto out;
  1240. }
  1241. }
  1242. cpos += alloc_size;
  1243. trunc_len -= alloc_size;
  1244. }
  1245. ocfs2_truncate_cluster_pages(inode, byte_start, byte_len);
  1246. out:
  1247. ocfs2_schedule_truncate_log_flush(osb, 1);
  1248. ocfs2_run_deallocs(osb, &dealloc);
  1249. return ret;
  1250. }
  1251. /*
  1252. * Parts of this function taken from xfs_change_file_space()
  1253. */
  1254. static int __ocfs2_change_file_space(struct file *file, struct inode *inode,
  1255. loff_t f_pos, unsigned int cmd,
  1256. struct ocfs2_space_resv *sr,
  1257. int change_size)
  1258. {
  1259. int ret;
  1260. s64 llen;
  1261. loff_t size;
  1262. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1263. struct buffer_head *di_bh = NULL;
  1264. handle_t *handle;
  1265. unsigned long long max_off = inode->i_sb->s_maxbytes;
  1266. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb))
  1267. return -EROFS;
  1268. mutex_lock(&inode->i_mutex);
  1269. /*
  1270. * This prevents concurrent writes on other nodes
  1271. */
  1272. ret = ocfs2_rw_lock(inode, 1);
  1273. if (ret) {
  1274. mlog_errno(ret);
  1275. goto out;
  1276. }
  1277. ret = ocfs2_inode_lock(inode, &di_bh, 1);
  1278. if (ret) {
  1279. mlog_errno(ret);
  1280. goto out_rw_unlock;
  1281. }
  1282. if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) {
  1283. ret = -EPERM;
  1284. goto out_inode_unlock;
  1285. }
  1286. switch (sr->l_whence) {
  1287. case 0: /*SEEK_SET*/
  1288. break;
  1289. case 1: /*SEEK_CUR*/
  1290. sr->l_start += f_pos;
  1291. break;
  1292. case 2: /*SEEK_END*/
  1293. sr->l_start += i_size_read(inode);
  1294. break;
  1295. default:
  1296. ret = -EINVAL;
  1297. goto out_inode_unlock;
  1298. }
  1299. sr->l_whence = 0;
  1300. llen = sr->l_len > 0 ? sr->l_len - 1 : sr->l_len;
  1301. if (sr->l_start < 0
  1302. || sr->l_start > max_off
  1303. || (sr->l_start + llen) < 0
  1304. || (sr->l_start + llen) > max_off) {
  1305. ret = -EINVAL;
  1306. goto out_inode_unlock;
  1307. }
  1308. size = sr->l_start + sr->l_len;
  1309. if (cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) {
  1310. if (sr->l_len <= 0) {
  1311. ret = -EINVAL;
  1312. goto out_inode_unlock;
  1313. }
  1314. }
  1315. if (file && should_remove_suid(file->f_path.dentry)) {
  1316. ret = __ocfs2_write_remove_suid(inode, di_bh);
  1317. if (ret) {
  1318. mlog_errno(ret);
  1319. goto out_inode_unlock;
  1320. }
  1321. }
  1322. down_write(&OCFS2_I(inode)->ip_alloc_sem);
  1323. switch (cmd) {
  1324. case OCFS2_IOC_RESVSP:
  1325. case OCFS2_IOC_RESVSP64:
  1326. /*
  1327. * This takes unsigned offsets, but the signed ones we
  1328. * pass have been checked against overflow above.
  1329. */
  1330. ret = ocfs2_allocate_unwritten_extents(inode, sr->l_start,
  1331. sr->l_len);
  1332. break;
  1333. case OCFS2_IOC_UNRESVSP:
  1334. case OCFS2_IOC_UNRESVSP64:
  1335. ret = ocfs2_remove_inode_range(inode, di_bh, sr->l_start,
  1336. sr->l_len);
  1337. break;
  1338. default:
  1339. ret = -EINVAL;
  1340. }
  1341. up_write(&OCFS2_I(inode)->ip_alloc_sem);
  1342. if (ret) {
  1343. mlog_errno(ret);
  1344. goto out_inode_unlock;
  1345. }
  1346. /*
  1347. * We update c/mtime for these changes
  1348. */
  1349. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  1350. if (IS_ERR(handle)) {
  1351. ret = PTR_ERR(handle);
  1352. mlog_errno(ret);
  1353. goto out_inode_unlock;
  1354. }
  1355. if (change_size && i_size_read(inode) < size)
  1356. i_size_write(inode, size);
  1357. inode->i_ctime = inode->i_mtime = CURRENT_TIME;
  1358. ret = ocfs2_mark_inode_dirty(handle, inode, di_bh);
  1359. if (ret < 0)
  1360. mlog_errno(ret);
  1361. ocfs2_commit_trans(osb, handle);
  1362. out_inode_unlock:
  1363. brelse(di_bh);
  1364. ocfs2_inode_unlock(inode, 1);
  1365. out_rw_unlock:
  1366. ocfs2_rw_unlock(inode, 1);
  1367. out:
  1368. mutex_unlock(&inode->i_mutex);
  1369. return ret;
  1370. }
  1371. int ocfs2_change_file_space(struct file *file, unsigned int cmd,
  1372. struct ocfs2_space_resv *sr)
  1373. {
  1374. struct inode *inode = file->f_path.dentry->d_inode;
  1375. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);;
  1376. if ((cmd == OCFS2_IOC_RESVSP || cmd == OCFS2_IOC_RESVSP64) &&
  1377. !ocfs2_writes_unwritten_extents(osb))
  1378. return -ENOTTY;
  1379. else if ((cmd == OCFS2_IOC_UNRESVSP || cmd == OCFS2_IOC_UNRESVSP64) &&
  1380. !ocfs2_sparse_alloc(osb))
  1381. return -ENOTTY;
  1382. if (!S_ISREG(inode->i_mode))
  1383. return -EINVAL;
  1384. if (!(file->f_mode & FMODE_WRITE))
  1385. return -EBADF;
  1386. return __ocfs2_change_file_space(file, inode, file->f_pos, cmd, sr, 0);
  1387. }
  1388. static long ocfs2_fallocate(struct inode *inode, int mode, loff_t offset,
  1389. loff_t len)
  1390. {
  1391. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1392. struct ocfs2_space_resv sr;
  1393. int change_size = 1;
  1394. if (!ocfs2_writes_unwritten_extents(osb))
  1395. return -EOPNOTSUPP;
  1396. if (S_ISDIR(inode->i_mode))
  1397. return -ENODEV;
  1398. if (mode & FALLOC_FL_KEEP_SIZE)
  1399. change_size = 0;
  1400. sr.l_whence = 0;
  1401. sr.l_start = (s64)offset;
  1402. sr.l_len = (s64)len;
  1403. return __ocfs2_change_file_space(NULL, inode, offset,
  1404. OCFS2_IOC_RESVSP64, &sr, change_size);
  1405. }
  1406. static int ocfs2_prepare_inode_for_write(struct dentry *dentry,
  1407. loff_t *ppos,
  1408. size_t count,
  1409. int appending,
  1410. int *direct_io)
  1411. {
  1412. int ret = 0, meta_level = 0;
  1413. struct inode *inode = dentry->d_inode;
  1414. loff_t saved_pos, end;
  1415. /*
  1416. * We start with a read level meta lock and only jump to an ex
  1417. * if we need to make modifications here.
  1418. */
  1419. for(;;) {
  1420. ret = ocfs2_inode_lock(inode, NULL, meta_level);
  1421. if (ret < 0) {
  1422. meta_level = -1;
  1423. mlog_errno(ret);
  1424. goto out;
  1425. }
  1426. /* Clear suid / sgid if necessary. We do this here
  1427. * instead of later in the write path because
  1428. * remove_suid() calls ->setattr without any hint that
  1429. * we may have already done our cluster locking. Since
  1430. * ocfs2_setattr() *must* take cluster locks to
  1431. * proceeed, this will lead us to recursively lock the
  1432. * inode. There's also the dinode i_size state which
  1433. * can be lost via setattr during extending writes (we
  1434. * set inode->i_size at the end of a write. */
  1435. if (should_remove_suid(dentry)) {
  1436. if (meta_level == 0) {
  1437. ocfs2_inode_unlock(inode, meta_level);
  1438. meta_level = 1;
  1439. continue;
  1440. }
  1441. ret = ocfs2_write_remove_suid(inode);
  1442. if (ret < 0) {
  1443. mlog_errno(ret);
  1444. goto out_unlock;
  1445. }
  1446. }
  1447. /* work on a copy of ppos until we're sure that we won't have
  1448. * to recalculate it due to relocking. */
  1449. if (appending) {
  1450. saved_pos = i_size_read(inode);
  1451. mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos);
  1452. } else {
  1453. saved_pos = *ppos;
  1454. }
  1455. end = saved_pos + count;
  1456. /*
  1457. * Skip the O_DIRECT checks if we don't need
  1458. * them.
  1459. */
  1460. if (!direct_io || !(*direct_io))
  1461. break;
  1462. /*
  1463. * There's no sane way to do direct writes to an inode
  1464. * with inline data.
  1465. */
  1466. if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
  1467. *direct_io = 0;
  1468. break;
  1469. }
  1470. /*
  1471. * Allowing concurrent direct writes means
  1472. * i_size changes wouldn't be synchronized, so
  1473. * one node could wind up truncating another
  1474. * nodes writes.
  1475. */
  1476. if (end > i_size_read(inode)) {
  1477. *direct_io = 0;
  1478. break;
  1479. }
  1480. /*
  1481. * We don't fill holes during direct io, so
  1482. * check for them here. If any are found, the
  1483. * caller will have to retake some cluster
  1484. * locks and initiate the io as buffered.
  1485. */
  1486. ret = ocfs2_check_range_for_holes(inode, saved_pos, count);
  1487. if (ret == 1) {
  1488. *direct_io = 0;
  1489. ret = 0;
  1490. } else if (ret < 0)
  1491. mlog_errno(ret);
  1492. break;
  1493. }
  1494. if (appending)
  1495. *ppos = saved_pos;
  1496. out_unlock:
  1497. ocfs2_inode_unlock(inode, meta_level);
  1498. out:
  1499. return ret;
  1500. }
  1501. static ssize_t ocfs2_file_aio_write(struct kiocb *iocb,
  1502. const struct iovec *iov,
  1503. unsigned long nr_segs,
  1504. loff_t pos)
  1505. {
  1506. int ret, direct_io, appending, rw_level, have_alloc_sem = 0;
  1507. int can_do_direct;
  1508. ssize_t written = 0;
  1509. size_t ocount; /* original count */
  1510. size_t count; /* after file limit checks */
  1511. loff_t old_size, *ppos = &iocb->ki_pos;
  1512. u32 old_clusters;
  1513. struct file *file = iocb->ki_filp;
  1514. struct inode *inode = file->f_path.dentry->d_inode;
  1515. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  1516. mlog_entry("(0x%p, %u, '%.*s')\n", file,
  1517. (unsigned int)nr_segs,
  1518. file->f_path.dentry->d_name.len,
  1519. file->f_path.dentry->d_name.name);
  1520. if (iocb->ki_left == 0)
  1521. return 0;
  1522. vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
  1523. appending = file->f_flags & O_APPEND ? 1 : 0;
  1524. direct_io = file->f_flags & O_DIRECT ? 1 : 0;
  1525. mutex_lock(&inode->i_mutex);
  1526. relock:
  1527. /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */
  1528. if (direct_io) {
  1529. down_read(&inode->i_alloc_sem);
  1530. have_alloc_sem = 1;
  1531. }
  1532. /* concurrent O_DIRECT writes are allowed */
  1533. rw_level = !direct_io;
  1534. ret = ocfs2_rw_lock(inode, rw_level);
  1535. if (ret < 0) {
  1536. mlog_errno(ret);
  1537. goto out_sems;
  1538. }
  1539. can_do_direct = direct_io;
  1540. ret = ocfs2_prepare_inode_for_write(file->f_path.dentry, ppos,
  1541. iocb->ki_left, appending,
  1542. &can_do_direct);
  1543. if (ret < 0) {
  1544. mlog_errno(ret);
  1545. goto out;
  1546. }
  1547. /*
  1548. * We can't complete the direct I/O as requested, fall back to
  1549. * buffered I/O.
  1550. */
  1551. if (direct_io && !can_do_direct) {
  1552. ocfs2_rw_unlock(inode, rw_level);
  1553. up_read(&inode->i_alloc_sem);
  1554. have_alloc_sem = 0;
  1555. rw_level = -1;
  1556. direct_io = 0;
  1557. goto relock;
  1558. }
  1559. /*
  1560. * To later detect whether a journal commit for sync writes is
  1561. * necessary, we sample i_size, and cluster count here.
  1562. */
  1563. old_size = i_size_read(inode);
  1564. old_clusters = OCFS2_I(inode)->ip_clusters;
  1565. /* communicate with ocfs2_dio_end_io */
  1566. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1567. if (direct_io) {
  1568. ret = generic_segment_checks(iov, &nr_segs, &ocount,
  1569. VERIFY_READ);
  1570. if (ret)
  1571. goto out_dio;
  1572. ret = generic_write_checks(file, ppos, &count,
  1573. S_ISBLK(inode->i_mode));
  1574. if (ret)
  1575. goto out_dio;
  1576. written = generic_file_direct_write(iocb, iov, &nr_segs, *ppos,
  1577. ppos, count, ocount);
  1578. if (written < 0) {
  1579. ret = written;
  1580. goto out_dio;
  1581. }
  1582. } else {
  1583. written = generic_file_aio_write_nolock(iocb, iov, nr_segs,
  1584. *ppos);
  1585. }
  1586. out_dio:
  1587. /* buffered aio wouldn't have proper lock coverage today */
  1588. BUG_ON(ret == -EIOCBQUEUED && !(file->f_flags & O_DIRECT));
  1589. if ((file->f_flags & O_SYNC && !direct_io) || IS_SYNC(inode)) {
  1590. /*
  1591. * The generic write paths have handled getting data
  1592. * to disk, but since we don't make use of the dirty
  1593. * inode list, a manual journal commit is necessary
  1594. * here.
  1595. */
  1596. if (old_size != i_size_read(inode) ||
  1597. old_clusters != OCFS2_I(inode)->ip_clusters) {
  1598. ret = journal_force_commit(osb->journal->j_journal);
  1599. if (ret < 0)
  1600. written = ret;
  1601. }
  1602. }
  1603. /*
  1604. * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io
  1605. * function pointer which is called when o_direct io completes so that
  1606. * it can unlock our rw lock. (it's the clustered equivalent of
  1607. * i_alloc_sem; protects truncate from racing with pending ios).
  1608. * Unfortunately there are error cases which call end_io and others
  1609. * that don't. so we don't have to unlock the rw_lock if either an
  1610. * async dio is going to do it in the future or an end_io after an
  1611. * error has already done it.
  1612. */
  1613. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1614. rw_level = -1;
  1615. have_alloc_sem = 0;
  1616. }
  1617. out:
  1618. if (rw_level != -1)
  1619. ocfs2_rw_unlock(inode, rw_level);
  1620. out_sems:
  1621. if (have_alloc_sem)
  1622. up_read(&inode->i_alloc_sem);
  1623. mutex_unlock(&inode->i_mutex);
  1624. mlog_exit(ret);
  1625. return written ? written : ret;
  1626. }
  1627. static ssize_t ocfs2_file_splice_write(struct pipe_inode_info *pipe,
  1628. struct file *out,
  1629. loff_t *ppos,
  1630. size_t len,
  1631. unsigned int flags)
  1632. {
  1633. int ret;
  1634. struct inode *inode = out->f_path.dentry->d_inode;
  1635. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", out, pipe,
  1636. (unsigned int)len,
  1637. out->f_path.dentry->d_name.len,
  1638. out->f_path.dentry->d_name.name);
  1639. inode_double_lock(inode, pipe->inode);
  1640. ret = ocfs2_rw_lock(inode, 1);
  1641. if (ret < 0) {
  1642. mlog_errno(ret);
  1643. goto out;
  1644. }
  1645. ret = ocfs2_prepare_inode_for_write(out->f_path.dentry, ppos, len, 0,
  1646. NULL);
  1647. if (ret < 0) {
  1648. mlog_errno(ret);
  1649. goto out_unlock;
  1650. }
  1651. ret = generic_file_splice_write_nolock(pipe, out, ppos, len, flags);
  1652. out_unlock:
  1653. ocfs2_rw_unlock(inode, 1);
  1654. out:
  1655. inode_double_unlock(inode, pipe->inode);
  1656. mlog_exit(ret);
  1657. return ret;
  1658. }
  1659. static ssize_t ocfs2_file_splice_read(struct file *in,
  1660. loff_t *ppos,
  1661. struct pipe_inode_info *pipe,
  1662. size_t len,
  1663. unsigned int flags)
  1664. {
  1665. int ret = 0;
  1666. struct inode *inode = in->f_path.dentry->d_inode;
  1667. mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", in, pipe,
  1668. (unsigned int)len,
  1669. in->f_path.dentry->d_name.len,
  1670. in->f_path.dentry->d_name.name);
  1671. /*
  1672. * See the comment in ocfs2_file_aio_read()
  1673. */
  1674. ret = ocfs2_inode_lock(inode, NULL, 0);
  1675. if (ret < 0) {
  1676. mlog_errno(ret);
  1677. goto bail;
  1678. }
  1679. ocfs2_inode_unlock(inode, 0);
  1680. ret = generic_file_splice_read(in, ppos, pipe, len, flags);
  1681. bail:
  1682. mlog_exit(ret);
  1683. return ret;
  1684. }
  1685. static ssize_t ocfs2_file_aio_read(struct kiocb *iocb,
  1686. const struct iovec *iov,
  1687. unsigned long nr_segs,
  1688. loff_t pos)
  1689. {
  1690. int ret = 0, rw_level = -1, have_alloc_sem = 0, lock_level = 0;
  1691. struct file *filp = iocb->ki_filp;
  1692. struct inode *inode = filp->f_path.dentry->d_inode;
  1693. mlog_entry("(0x%p, %u, '%.*s')\n", filp,
  1694. (unsigned int)nr_segs,
  1695. filp->f_path.dentry->d_name.len,
  1696. filp->f_path.dentry->d_name.name);
  1697. if (!inode) {
  1698. ret = -EINVAL;
  1699. mlog_errno(ret);
  1700. goto bail;
  1701. }
  1702. /*
  1703. * buffered reads protect themselves in ->readpage(). O_DIRECT reads
  1704. * need locks to protect pending reads from racing with truncate.
  1705. */
  1706. if (filp->f_flags & O_DIRECT) {
  1707. down_read(&inode->i_alloc_sem);
  1708. have_alloc_sem = 1;
  1709. ret = ocfs2_rw_lock(inode, 0);
  1710. if (ret < 0) {
  1711. mlog_errno(ret);
  1712. goto bail;
  1713. }
  1714. rw_level = 0;
  1715. /* communicate with ocfs2_dio_end_io */
  1716. ocfs2_iocb_set_rw_locked(iocb, rw_level);
  1717. }
  1718. /*
  1719. * We're fine letting folks race truncates and extending
  1720. * writes with read across the cluster, just like they can
  1721. * locally. Hence no rw_lock during read.
  1722. *
  1723. * Take and drop the meta data lock to update inode fields
  1724. * like i_size. This allows the checks down below
  1725. * generic_file_aio_read() a chance of actually working.
  1726. */
  1727. ret = ocfs2_inode_lock_atime(inode, filp->f_vfsmnt, &lock_level);
  1728. if (ret < 0) {
  1729. mlog_errno(ret);
  1730. goto bail;
  1731. }
  1732. ocfs2_inode_unlock(inode, lock_level);
  1733. ret = generic_file_aio_read(iocb, iov, nr_segs, iocb->ki_pos);
  1734. if (ret == -EINVAL)
  1735. mlog(0, "generic_file_aio_read returned -EINVAL\n");
  1736. /* buffered aio wouldn't have proper lock coverage today */
  1737. BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT));
  1738. /* see ocfs2_file_aio_write */
  1739. if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) {
  1740. rw_level = -1;
  1741. have_alloc_sem = 0;
  1742. }
  1743. bail:
  1744. if (have_alloc_sem)
  1745. up_read(&inode->i_alloc_sem);
  1746. if (rw_level != -1)
  1747. ocfs2_rw_unlock(inode, rw_level);
  1748. mlog_exit(ret);
  1749. return ret;
  1750. }
  1751. const struct inode_operations ocfs2_file_iops = {
  1752. .setattr = ocfs2_setattr,
  1753. .getattr = ocfs2_getattr,
  1754. .permission = ocfs2_permission,
  1755. .setxattr = generic_setxattr,
  1756. .getxattr = generic_getxattr,
  1757. .listxattr = ocfs2_listxattr,
  1758. .removexattr = generic_removexattr,
  1759. .fallocate = ocfs2_fallocate,
  1760. .fiemap = ocfs2_fiemap,
  1761. };
  1762. const struct inode_operations ocfs2_special_file_iops = {
  1763. .setattr = ocfs2_setattr,
  1764. .getattr = ocfs2_getattr,
  1765. .permission = ocfs2_permission,
  1766. };
  1767. /*
  1768. * Other than ->lock, keep ocfs2_fops and ocfs2_dops in sync with
  1769. * ocfs2_fops_no_plocks and ocfs2_dops_no_plocks!
  1770. */
  1771. const struct file_operations ocfs2_fops = {
  1772. .llseek = generic_file_llseek,
  1773. .read = do_sync_read,
  1774. .write = do_sync_write,
  1775. .mmap = ocfs2_mmap,
  1776. .fsync = ocfs2_sync_file,
  1777. .release = ocfs2_file_release,
  1778. .open = ocfs2_file_open,
  1779. .aio_read = ocfs2_file_aio_read,
  1780. .aio_write = ocfs2_file_aio_write,
  1781. .unlocked_ioctl = ocfs2_ioctl,
  1782. #ifdef CONFIG_COMPAT
  1783. .compat_ioctl = ocfs2_compat_ioctl,
  1784. #endif
  1785. .lock = ocfs2_lock,
  1786. .flock = ocfs2_flock,
  1787. .splice_read = ocfs2_file_splice_read,
  1788. .splice_write = ocfs2_file_splice_write,
  1789. };
  1790. const struct file_operations ocfs2_dops = {
  1791. .llseek = generic_file_llseek,
  1792. .read = generic_read_dir,
  1793. .readdir = ocfs2_readdir,
  1794. .fsync = ocfs2_sync_file,
  1795. .release = ocfs2_dir_release,
  1796. .open = ocfs2_dir_open,
  1797. .unlocked_ioctl = ocfs2_ioctl,
  1798. #ifdef CONFIG_COMPAT
  1799. .compat_ioctl = ocfs2_compat_ioctl,
  1800. #endif
  1801. .lock = ocfs2_lock,
  1802. .flock = ocfs2_flock,
  1803. };
  1804. /*
  1805. * POSIX-lockless variants of our file_operations.
  1806. *
  1807. * These will be used if the underlying cluster stack does not support
  1808. * posix file locking, if the user passes the "localflocks" mount
  1809. * option, or if we have a local-only fs.
  1810. *
  1811. * ocfs2_flock is in here because all stacks handle UNIX file locks,
  1812. * so we still want it in the case of no stack support for
  1813. * plocks. Internally, it will do the right thing when asked to ignore
  1814. * the cluster.
  1815. */
  1816. const struct file_operations ocfs2_fops_no_plocks = {
  1817. .llseek = generic_file_llseek,
  1818. .read = do_sync_read,
  1819. .write = do_sync_write,
  1820. .mmap = ocfs2_mmap,
  1821. .fsync = ocfs2_sync_file,
  1822. .release = ocfs2_file_release,
  1823. .open = ocfs2_file_open,
  1824. .aio_read = ocfs2_file_aio_read,
  1825. .aio_write = ocfs2_file_aio_write,
  1826. .unlocked_ioctl = ocfs2_ioctl,
  1827. #ifdef CONFIG_COMPAT
  1828. .compat_ioctl = ocfs2_compat_ioctl,
  1829. #endif
  1830. .flock = ocfs2_flock,
  1831. .splice_read = ocfs2_file_splice_read,
  1832. .splice_write = ocfs2_file_splice_write,
  1833. };
  1834. const struct file_operations ocfs2_dops_no_plocks = {
  1835. .llseek = generic_file_llseek,
  1836. .read = generic_read_dir,
  1837. .readdir = ocfs2_readdir,
  1838. .fsync = ocfs2_sync_file,
  1839. .release = ocfs2_dir_release,
  1840. .open = ocfs2_dir_open,
  1841. .unlocked_ioctl = ocfs2_ioctl,
  1842. #ifdef CONFIG_COMPAT
  1843. .compat_ioctl = ocfs2_compat_ioctl,
  1844. #endif
  1845. .flock = ocfs2_flock,
  1846. };