file.c 56 KB

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