file.c 61 KB

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