xfs_vnodeops.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_da_btree.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_inode_item.h"
  39. #include "xfs_itable.h"
  40. #include "xfs_btree.h"
  41. #include "xfs_ialloc.h"
  42. #include "xfs_alloc.h"
  43. #include "xfs_bmap.h"
  44. #include "xfs_attr.h"
  45. #include "xfs_rw.h"
  46. #include "xfs_error.h"
  47. #include "xfs_quota.h"
  48. #include "xfs_utils.h"
  49. #include "xfs_rtalloc.h"
  50. #include "xfs_trans_space.h"
  51. #include "xfs_log_priv.h"
  52. #include "xfs_filestream.h"
  53. #include "xfs_vnodeops.h"
  54. int
  55. xfs_open(
  56. xfs_inode_t *ip)
  57. {
  58. int mode;
  59. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  60. return XFS_ERROR(EIO);
  61. /*
  62. * If it's a directory with any blocks, read-ahead block 0
  63. * as we're almost certain to have the next operation be a read there.
  64. */
  65. if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) {
  66. mode = xfs_ilock_map_shared(ip);
  67. if (ip->i_d.di_nextents > 0)
  68. (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  69. xfs_iunlock(ip, mode);
  70. }
  71. return 0;
  72. }
  73. int
  74. xfs_setattr(
  75. struct xfs_inode *ip,
  76. struct iattr *iattr,
  77. int flags,
  78. cred_t *credp)
  79. {
  80. xfs_mount_t *mp = ip->i_mount;
  81. struct inode *inode = VFS_I(ip);
  82. int mask = iattr->ia_valid;
  83. xfs_trans_t *tp;
  84. int code;
  85. uint lock_flags;
  86. uint commit_flags=0;
  87. uid_t uid=0, iuid=0;
  88. gid_t gid=0, igid=0;
  89. int timeflags = 0;
  90. struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
  91. int file_owner;
  92. int need_iolock = 1;
  93. xfs_itrace_entry(ip);
  94. if (mp->m_flags & XFS_MOUNT_RDONLY)
  95. return XFS_ERROR(EROFS);
  96. if (XFS_FORCED_SHUTDOWN(mp))
  97. return XFS_ERROR(EIO);
  98. olddquot1 = olddquot2 = NULL;
  99. udqp = gdqp = NULL;
  100. /*
  101. * If disk quotas is on, we make sure that the dquots do exist on disk,
  102. * before we start any other transactions. Trying to do this later
  103. * is messy. We don't care to take a readlock to look at the ids
  104. * in inode here, because we can't hold it across the trans_reserve.
  105. * If the IDs do change before we take the ilock, we're covered
  106. * because the i_*dquot fields will get updated anyway.
  107. */
  108. if (XFS_IS_QUOTA_ON(mp) && (mask & (ATTR_UID|ATTR_GID))) {
  109. uint qflags = 0;
  110. if ((mask & ATTR_UID) && XFS_IS_UQUOTA_ON(mp)) {
  111. uid = iattr->ia_uid;
  112. qflags |= XFS_QMOPT_UQUOTA;
  113. } else {
  114. uid = ip->i_d.di_uid;
  115. }
  116. if ((mask & ATTR_GID) && XFS_IS_GQUOTA_ON(mp)) {
  117. gid = iattr->ia_gid;
  118. qflags |= XFS_QMOPT_GQUOTA;
  119. } else {
  120. gid = ip->i_d.di_gid;
  121. }
  122. /*
  123. * We take a reference when we initialize udqp and gdqp,
  124. * so it is important that we never blindly double trip on
  125. * the same variable. See xfs_create() for an example.
  126. */
  127. ASSERT(udqp == NULL);
  128. ASSERT(gdqp == NULL);
  129. code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, ip->i_d.di_projid,
  130. qflags, &udqp, &gdqp);
  131. if (code)
  132. return code;
  133. }
  134. /*
  135. * For the other attributes, we acquire the inode lock and
  136. * first do an error checking pass.
  137. */
  138. tp = NULL;
  139. lock_flags = XFS_ILOCK_EXCL;
  140. if (flags & XFS_ATTR_NOLOCK)
  141. need_iolock = 0;
  142. if (!(mask & ATTR_SIZE)) {
  143. if ((mask != (ATTR_CTIME|ATTR_ATIME|ATTR_MTIME)) ||
  144. (mp->m_flags & XFS_MOUNT_WSYNC)) {
  145. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  146. commit_flags = 0;
  147. if ((code = xfs_trans_reserve(tp, 0,
  148. XFS_ICHANGE_LOG_RES(mp), 0,
  149. 0, 0))) {
  150. lock_flags = 0;
  151. goto error_return;
  152. }
  153. }
  154. } else {
  155. if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
  156. !(flags & XFS_ATTR_DMI)) {
  157. int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
  158. code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
  159. iattr->ia_size, 0, dmflags, NULL);
  160. if (code) {
  161. lock_flags = 0;
  162. goto error_return;
  163. }
  164. }
  165. if (need_iolock)
  166. lock_flags |= XFS_IOLOCK_EXCL;
  167. }
  168. xfs_ilock(ip, lock_flags);
  169. /* boolean: are we the file owner? */
  170. file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
  171. /*
  172. * Change various properties of a file.
  173. * Only the owner or users with CAP_FOWNER
  174. * capability may do these things.
  175. */
  176. if (mask & (ATTR_MODE|ATTR_UID|ATTR_GID)) {
  177. /*
  178. * CAP_FOWNER overrides the following restrictions:
  179. *
  180. * The user ID of the calling process must be equal
  181. * to the file owner ID, except in cases where the
  182. * CAP_FSETID capability is applicable.
  183. */
  184. if (!file_owner && !capable(CAP_FOWNER)) {
  185. code = XFS_ERROR(EPERM);
  186. goto error_return;
  187. }
  188. /*
  189. * CAP_FSETID overrides the following restrictions:
  190. *
  191. * The effective user ID of the calling process shall match
  192. * the file owner when setting the set-user-ID and
  193. * set-group-ID bits on that file.
  194. *
  195. * The effective group ID or one of the supplementary group
  196. * IDs of the calling process shall match the group owner of
  197. * the file when setting the set-group-ID bit on that file
  198. */
  199. if (mask & ATTR_MODE) {
  200. mode_t m = 0;
  201. if ((iattr->ia_mode & S_ISUID) && !file_owner)
  202. m |= S_ISUID;
  203. if ((iattr->ia_mode & S_ISGID) &&
  204. !in_group_p((gid_t)ip->i_d.di_gid))
  205. m |= S_ISGID;
  206. #if 0
  207. /* Linux allows this, Irix doesn't. */
  208. if ((iattr->ia_mode & S_ISVTX) && !S_ISDIR(ip->i_d.di_mode))
  209. m |= S_ISVTX;
  210. #endif
  211. if (m && !capable(CAP_FSETID))
  212. iattr->ia_mode &= ~m;
  213. }
  214. }
  215. /*
  216. * Change file ownership. Must be the owner or privileged.
  217. * If the system was configured with the "restricted_chown"
  218. * option, the owner is not permitted to give away the file,
  219. * and can change the group id only to a group of which he
  220. * or she is a member.
  221. */
  222. if (mask & (ATTR_UID|ATTR_GID)) {
  223. /*
  224. * These IDs could have changed since we last looked at them.
  225. * But, we're assured that if the ownership did change
  226. * while we didn't have the inode locked, inode's dquot(s)
  227. * would have changed also.
  228. */
  229. iuid = ip->i_d.di_uid;
  230. igid = ip->i_d.di_gid;
  231. gid = (mask & ATTR_GID) ? iattr->ia_gid : igid;
  232. uid = (mask & ATTR_UID) ? iattr->ia_uid : iuid;
  233. /*
  234. * CAP_CHOWN overrides the following restrictions:
  235. *
  236. * If _POSIX_CHOWN_RESTRICTED is defined, this capability
  237. * shall override the restriction that a process cannot
  238. * change the user ID of a file it owns and the restriction
  239. * that the group ID supplied to the chown() function
  240. * shall be equal to either the group ID or one of the
  241. * supplementary group IDs of the calling process.
  242. */
  243. if (restricted_chown &&
  244. (iuid != uid || (igid != gid &&
  245. !in_group_p((gid_t)gid))) &&
  246. !capable(CAP_CHOWN)) {
  247. code = XFS_ERROR(EPERM);
  248. goto error_return;
  249. }
  250. /*
  251. * Do a quota reservation only if uid/gid is actually
  252. * going to change.
  253. */
  254. if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
  255. (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
  256. ASSERT(tp);
  257. code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
  258. capable(CAP_FOWNER) ?
  259. XFS_QMOPT_FORCE_RES : 0);
  260. if (code) /* out of quota */
  261. goto error_return;
  262. }
  263. }
  264. /*
  265. * Truncate file. Must have write permission and not be a directory.
  266. */
  267. if (mask & ATTR_SIZE) {
  268. /* Short circuit the truncate case for zero length files */
  269. if (iattr->ia_size == 0 &&
  270. ip->i_size == 0 && ip->i_d.di_nextents == 0) {
  271. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  272. lock_flags &= ~XFS_ILOCK_EXCL;
  273. if (mask & ATTR_CTIME)
  274. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  275. code = 0;
  276. goto error_return;
  277. }
  278. if (S_ISDIR(ip->i_d.di_mode)) {
  279. code = XFS_ERROR(EISDIR);
  280. goto error_return;
  281. } else if (!S_ISREG(ip->i_d.di_mode)) {
  282. code = XFS_ERROR(EINVAL);
  283. goto error_return;
  284. }
  285. /*
  286. * Make sure that the dquots are attached to the inode.
  287. */
  288. if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
  289. goto error_return;
  290. }
  291. /*
  292. * Change file access or modified times.
  293. */
  294. if (mask & (ATTR_ATIME|ATTR_MTIME)) {
  295. if (!file_owner) {
  296. if ((mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)) &&
  297. !capable(CAP_FOWNER)) {
  298. code = XFS_ERROR(EPERM);
  299. goto error_return;
  300. }
  301. }
  302. }
  303. /*
  304. * Now we can make the changes. Before we join the inode
  305. * to the transaction, if ATTR_SIZE is set then take care of
  306. * the part of the truncation that must be done without the
  307. * inode lock. This needs to be done before joining the inode
  308. * to the transaction, because the inode cannot be unlocked
  309. * once it is a part of the transaction.
  310. */
  311. if (mask & ATTR_SIZE) {
  312. code = 0;
  313. if (iattr->ia_size > ip->i_size) {
  314. /*
  315. * Do the first part of growing a file: zero any data
  316. * in the last block that is beyond the old EOF. We
  317. * need to do this before the inode is joined to the
  318. * transaction to modify the i_size.
  319. */
  320. code = xfs_zero_eof(ip, iattr->ia_size, ip->i_size);
  321. }
  322. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  323. /*
  324. * We are going to log the inode size change in this
  325. * transaction so any previous writes that are beyond the on
  326. * disk EOF and the new EOF that have not been written out need
  327. * to be written here. If we do not write the data out, we
  328. * expose ourselves to the null files problem.
  329. *
  330. * Only flush from the on disk size to the smaller of the in
  331. * memory file size or the new size as that's the range we
  332. * really care about here and prevents waiting for other data
  333. * not within the range we care about here.
  334. */
  335. if (!code &&
  336. ip->i_size != ip->i_d.di_size &&
  337. iattr->ia_size > ip->i_d.di_size) {
  338. code = xfs_flush_pages(ip,
  339. ip->i_d.di_size, iattr->ia_size,
  340. XFS_B_ASYNC, FI_NONE);
  341. }
  342. /* wait for all I/O to complete */
  343. vn_iowait(ip);
  344. if (!code)
  345. code = xfs_itruncate_data(ip, iattr->ia_size);
  346. if (code) {
  347. ASSERT(tp == NULL);
  348. lock_flags &= ~XFS_ILOCK_EXCL;
  349. ASSERT(lock_flags == XFS_IOLOCK_EXCL);
  350. goto error_return;
  351. }
  352. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
  353. if ((code = xfs_trans_reserve(tp, 0,
  354. XFS_ITRUNCATE_LOG_RES(mp), 0,
  355. XFS_TRANS_PERM_LOG_RES,
  356. XFS_ITRUNCATE_LOG_COUNT))) {
  357. xfs_trans_cancel(tp, 0);
  358. if (need_iolock)
  359. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  360. return code;
  361. }
  362. commit_flags = XFS_TRANS_RELEASE_LOG_RES;
  363. xfs_ilock(ip, XFS_ILOCK_EXCL);
  364. }
  365. if (tp) {
  366. xfs_trans_ijoin(tp, ip, lock_flags);
  367. xfs_trans_ihold(tp, ip);
  368. }
  369. /*
  370. * Truncate file. Must have write permission and not be a directory.
  371. */
  372. if (mask & ATTR_SIZE) {
  373. /*
  374. * Only change the c/mtime if we are changing the size
  375. * or we are explicitly asked to change it. This handles
  376. * the semantic difference between truncate() and ftruncate()
  377. * as implemented in the VFS.
  378. */
  379. if (iattr->ia_size != ip->i_size || (mask & ATTR_CTIME))
  380. timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  381. if (iattr->ia_size > ip->i_size) {
  382. ip->i_d.di_size = iattr->ia_size;
  383. ip->i_size = iattr->ia_size;
  384. if (!(flags & XFS_ATTR_DMI))
  385. xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
  386. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  387. } else if (iattr->ia_size <= ip->i_size ||
  388. (iattr->ia_size == 0 && ip->i_d.di_nextents)) {
  389. /*
  390. * signal a sync transaction unless
  391. * we're truncating an already unlinked
  392. * file on a wsync filesystem
  393. */
  394. code = xfs_itruncate_finish(&tp, ip, iattr->ia_size,
  395. XFS_DATA_FORK,
  396. ((ip->i_d.di_nlink != 0 ||
  397. !(mp->m_flags & XFS_MOUNT_WSYNC))
  398. ? 1 : 0));
  399. if (code)
  400. goto abort_return;
  401. /*
  402. * Truncated "down", so we're removing references
  403. * to old data here - if we now delay flushing for
  404. * a long time, we expose ourselves unduly to the
  405. * notorious NULL files problem. So, we mark this
  406. * vnode and flush it when the file is closed, and
  407. * do not wait the usual (long) time for writeout.
  408. */
  409. xfs_iflags_set(ip, XFS_ITRUNCATED);
  410. }
  411. }
  412. /*
  413. * Change file access modes.
  414. */
  415. if (mask & ATTR_MODE) {
  416. ip->i_d.di_mode &= S_IFMT;
  417. ip->i_d.di_mode |= iattr->ia_mode & ~S_IFMT;
  418. inode->i_mode &= S_IFMT;
  419. inode->i_mode |= iattr->ia_mode & ~S_IFMT;
  420. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  421. timeflags |= XFS_ICHGTIME_CHG;
  422. }
  423. /*
  424. * Change file ownership. Must be the owner or privileged.
  425. * If the system was configured with the "restricted_chown"
  426. * option, the owner is not permitted to give away the file,
  427. * and can change the group id only to a group of which he
  428. * or she is a member.
  429. */
  430. if (mask & (ATTR_UID|ATTR_GID)) {
  431. /*
  432. * CAP_FSETID overrides the following restrictions:
  433. *
  434. * The set-user-ID and set-group-ID bits of a file will be
  435. * cleared upon successful return from chown()
  436. */
  437. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  438. !capable(CAP_FSETID)) {
  439. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  440. }
  441. /*
  442. * Change the ownerships and register quota modifications
  443. * in the transaction.
  444. */
  445. if (iuid != uid) {
  446. if (XFS_IS_UQUOTA_ON(mp)) {
  447. ASSERT(mask & ATTR_UID);
  448. ASSERT(udqp);
  449. olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  450. &ip->i_udquot, udqp);
  451. }
  452. ip->i_d.di_uid = uid;
  453. inode->i_uid = uid;
  454. }
  455. if (igid != gid) {
  456. if (XFS_IS_GQUOTA_ON(mp)) {
  457. ASSERT(!XFS_IS_PQUOTA_ON(mp));
  458. ASSERT(mask & ATTR_GID);
  459. ASSERT(gdqp);
  460. olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  461. &ip->i_gdquot, gdqp);
  462. }
  463. ip->i_d.di_gid = gid;
  464. inode->i_gid = gid;
  465. }
  466. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  467. timeflags |= XFS_ICHGTIME_CHG;
  468. }
  469. /*
  470. * Change file access or modified times.
  471. */
  472. if (mask & (ATTR_ATIME|ATTR_MTIME)) {
  473. if (mask & ATTR_ATIME) {
  474. inode->i_atime = iattr->ia_atime;
  475. ip->i_d.di_atime.t_sec = iattr->ia_atime.tv_sec;
  476. ip->i_d.di_atime.t_nsec = iattr->ia_atime.tv_nsec;
  477. ip->i_update_core = 1;
  478. }
  479. if (mask & ATTR_MTIME) {
  480. inode->i_mtime = iattr->ia_mtime;
  481. ip->i_d.di_mtime.t_sec = iattr->ia_mtime.tv_sec;
  482. ip->i_d.di_mtime.t_nsec = iattr->ia_mtime.tv_nsec;
  483. timeflags &= ~XFS_ICHGTIME_MOD;
  484. timeflags |= XFS_ICHGTIME_CHG;
  485. }
  486. if (tp && (mask & (ATTR_MTIME_SET|ATTR_ATIME_SET)))
  487. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  488. }
  489. /*
  490. * Change file inode change time only if ATTR_CTIME set
  491. * AND we have been called by a DMI function.
  492. */
  493. if ((flags & XFS_ATTR_DMI) && (mask & ATTR_CTIME)) {
  494. inode->i_ctime = iattr->ia_ctime;
  495. ip->i_d.di_ctime.t_sec = iattr->ia_ctime.tv_sec;
  496. ip->i_d.di_ctime.t_nsec = iattr->ia_ctime.tv_nsec;
  497. ip->i_update_core = 1;
  498. timeflags &= ~XFS_ICHGTIME_CHG;
  499. }
  500. /*
  501. * Send out timestamp changes that need to be set to the
  502. * current time. Not done when called by a DMI function.
  503. */
  504. if (timeflags && !(flags & XFS_ATTR_DMI))
  505. xfs_ichgtime(ip, timeflags);
  506. XFS_STATS_INC(xs_ig_attrchg);
  507. /*
  508. * If this is a synchronous mount, make sure that the
  509. * transaction goes to disk before returning to the user.
  510. * This is slightly sub-optimal in that truncates require
  511. * two sync transactions instead of one for wsync filesystems.
  512. * One for the truncate and one for the timestamps since we
  513. * don't want to change the timestamps unless we're sure the
  514. * truncate worked. Truncates are less than 1% of the laddis
  515. * mix so this probably isn't worth the trouble to optimize.
  516. */
  517. code = 0;
  518. if (tp) {
  519. if (mp->m_flags & XFS_MOUNT_WSYNC)
  520. xfs_trans_set_sync(tp);
  521. code = xfs_trans_commit(tp, commit_flags);
  522. }
  523. xfs_iunlock(ip, lock_flags);
  524. /*
  525. * Release any dquot(s) the inode had kept before chown.
  526. */
  527. XFS_QM_DQRELE(mp, olddquot1);
  528. XFS_QM_DQRELE(mp, olddquot2);
  529. XFS_QM_DQRELE(mp, udqp);
  530. XFS_QM_DQRELE(mp, gdqp);
  531. if (code) {
  532. return code;
  533. }
  534. if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
  535. !(flags & XFS_ATTR_DMI)) {
  536. (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
  537. NULL, DM_RIGHT_NULL, NULL, NULL,
  538. 0, 0, AT_DELAY_FLAG(flags));
  539. }
  540. return 0;
  541. abort_return:
  542. commit_flags |= XFS_TRANS_ABORT;
  543. /* FALLTHROUGH */
  544. error_return:
  545. XFS_QM_DQRELE(mp, udqp);
  546. XFS_QM_DQRELE(mp, gdqp);
  547. if (tp) {
  548. xfs_trans_cancel(tp, commit_flags);
  549. }
  550. if (lock_flags != 0) {
  551. xfs_iunlock(ip, lock_flags);
  552. }
  553. return code;
  554. }
  555. /*
  556. * The maximum pathlen is 1024 bytes. Since the minimum file system
  557. * blocksize is 512 bytes, we can get a max of 2 extents back from
  558. * bmapi.
  559. */
  560. #define SYMLINK_MAPS 2
  561. STATIC int
  562. xfs_readlink_bmap(
  563. xfs_inode_t *ip,
  564. char *link)
  565. {
  566. xfs_mount_t *mp = ip->i_mount;
  567. int pathlen = ip->i_d.di_size;
  568. int nmaps = SYMLINK_MAPS;
  569. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  570. xfs_daddr_t d;
  571. int byte_cnt;
  572. int n;
  573. xfs_buf_t *bp;
  574. int error = 0;
  575. error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
  576. mval, &nmaps, NULL, NULL);
  577. if (error)
  578. goto out;
  579. for (n = 0; n < nmaps; n++) {
  580. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  581. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  582. bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
  583. error = XFS_BUF_GETERROR(bp);
  584. if (error) {
  585. xfs_ioerror_alert("xfs_readlink",
  586. ip->i_mount, bp, XFS_BUF_ADDR(bp));
  587. xfs_buf_relse(bp);
  588. goto out;
  589. }
  590. if (pathlen < byte_cnt)
  591. byte_cnt = pathlen;
  592. pathlen -= byte_cnt;
  593. memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
  594. xfs_buf_relse(bp);
  595. }
  596. link[ip->i_d.di_size] = '\0';
  597. error = 0;
  598. out:
  599. return error;
  600. }
  601. int
  602. xfs_readlink(
  603. xfs_inode_t *ip,
  604. char *link)
  605. {
  606. xfs_mount_t *mp = ip->i_mount;
  607. int pathlen;
  608. int error = 0;
  609. xfs_itrace_entry(ip);
  610. if (XFS_FORCED_SHUTDOWN(mp))
  611. return XFS_ERROR(EIO);
  612. xfs_ilock(ip, XFS_ILOCK_SHARED);
  613. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
  614. ASSERT(ip->i_d.di_size <= MAXPATHLEN);
  615. pathlen = ip->i_d.di_size;
  616. if (!pathlen)
  617. goto out;
  618. if (ip->i_df.if_flags & XFS_IFINLINE) {
  619. memcpy(link, ip->i_df.if_u1.if_data, pathlen);
  620. link[pathlen] = '\0';
  621. } else {
  622. error = xfs_readlink_bmap(ip, link);
  623. }
  624. out:
  625. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  626. return error;
  627. }
  628. /*
  629. * xfs_fsync
  630. *
  631. * This is called to sync the inode and its data out to disk. We need to hold
  632. * the I/O lock while flushing the data, and the inode lock while flushing the
  633. * inode. The inode lock CANNOT be held while flushing the data, so acquire
  634. * after we're done with that.
  635. */
  636. int
  637. xfs_fsync(
  638. xfs_inode_t *ip)
  639. {
  640. xfs_trans_t *tp;
  641. int error;
  642. int log_flushed = 0, changed = 1;
  643. xfs_itrace_entry(ip);
  644. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  645. return XFS_ERROR(EIO);
  646. /* capture size updates in I/O completion before writing the inode. */
  647. error = filemap_fdatawait(VFS_I(ip)->i_mapping);
  648. if (error)
  649. return XFS_ERROR(error);
  650. /*
  651. * We always need to make sure that the required inode state is safe on
  652. * disk. The vnode might be clean but we still might need to force the
  653. * log because of committed transactions that haven't hit the disk yet.
  654. * Likewise, there could be unflushed non-transactional changes to the
  655. * inode core that have to go to disk and this requires us to issue
  656. * a synchronous transaction to capture these changes correctly.
  657. *
  658. * This code relies on the assumption that if the update_* fields
  659. * of the inode are clear and the inode is unpinned then it is clean
  660. * and no action is required.
  661. */
  662. xfs_ilock(ip, XFS_ILOCK_SHARED);
  663. if (!(ip->i_update_size || ip->i_update_core)) {
  664. /*
  665. * Timestamps/size haven't changed since last inode flush or
  666. * inode transaction commit. That means either nothing got
  667. * written or a transaction committed which caught the updates.
  668. * If the latter happened and the transaction hasn't hit the
  669. * disk yet, the inode will be still be pinned. If it is,
  670. * force the log.
  671. */
  672. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  673. if (xfs_ipincount(ip)) {
  674. error = _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
  675. XFS_LOG_FORCE | XFS_LOG_SYNC,
  676. &log_flushed);
  677. } else {
  678. /*
  679. * If the inode is not pinned and nothing has changed
  680. * we don't need to flush the cache.
  681. */
  682. changed = 0;
  683. }
  684. } else {
  685. /*
  686. * Kick off a transaction to log the inode core to get the
  687. * updates. The sync transaction will also force the log.
  688. */
  689. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  690. tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
  691. error = xfs_trans_reserve(tp, 0,
  692. XFS_FSYNC_TS_LOG_RES(ip->i_mount), 0, 0, 0);
  693. if (error) {
  694. xfs_trans_cancel(tp, 0);
  695. return error;
  696. }
  697. xfs_ilock(ip, XFS_ILOCK_EXCL);
  698. /*
  699. * Note - it's possible that we might have pushed ourselves out
  700. * of the way during trans_reserve which would flush the inode.
  701. * But there's no guarantee that the inode buffer has actually
  702. * gone out yet (it's delwri). Plus the buffer could be pinned
  703. * anyway if it's part of an inode in another recent
  704. * transaction. So we play it safe and fire off the
  705. * transaction anyway.
  706. */
  707. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  708. xfs_trans_ihold(tp, ip);
  709. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  710. xfs_trans_set_sync(tp);
  711. error = _xfs_trans_commit(tp, 0, &log_flushed);
  712. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  713. }
  714. if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
  715. /*
  716. * If the log write didn't issue an ordered tag we need
  717. * to flush the disk cache for the data device now.
  718. */
  719. if (!log_flushed)
  720. xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
  721. /*
  722. * If this inode is on the RT dev we need to flush that
  723. * cache as well.
  724. */
  725. if (XFS_IS_REALTIME_INODE(ip))
  726. xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
  727. }
  728. return error;
  729. }
  730. /*
  731. * This is called by xfs_inactive to free any blocks beyond eof
  732. * when the link count isn't zero and by xfs_dm_punch_hole() when
  733. * punching a hole to EOF.
  734. */
  735. int
  736. xfs_free_eofblocks(
  737. xfs_mount_t *mp,
  738. xfs_inode_t *ip,
  739. int flags)
  740. {
  741. xfs_trans_t *tp;
  742. int error;
  743. xfs_fileoff_t end_fsb;
  744. xfs_fileoff_t last_fsb;
  745. xfs_filblks_t map_len;
  746. int nimaps;
  747. xfs_bmbt_irec_t imap;
  748. int use_iolock = (flags & XFS_FREE_EOF_LOCK);
  749. /*
  750. * Figure out if there are any blocks beyond the end
  751. * of the file. If not, then there is nothing to do.
  752. */
  753. end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
  754. last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  755. map_len = last_fsb - end_fsb;
  756. if (map_len <= 0)
  757. return 0;
  758. nimaps = 1;
  759. xfs_ilock(ip, XFS_ILOCK_SHARED);
  760. error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
  761. NULL, 0, &imap, &nimaps, NULL, NULL);
  762. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  763. if (!error && (nimaps != 0) &&
  764. (imap.br_startblock != HOLESTARTBLOCK ||
  765. ip->i_delayed_blks)) {
  766. /*
  767. * Attach the dquots to the inode up front.
  768. */
  769. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  770. return error;
  771. /*
  772. * There are blocks after the end of file.
  773. * Free them up now by truncating the file to
  774. * its current size.
  775. */
  776. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  777. /*
  778. * Do the xfs_itruncate_start() call before
  779. * reserving any log space because
  780. * itruncate_start will call into the buffer
  781. * cache and we can't
  782. * do that within a transaction.
  783. */
  784. if (use_iolock)
  785. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  786. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
  787. ip->i_size);
  788. if (error) {
  789. xfs_trans_cancel(tp, 0);
  790. if (use_iolock)
  791. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  792. return error;
  793. }
  794. error = xfs_trans_reserve(tp, 0,
  795. XFS_ITRUNCATE_LOG_RES(mp),
  796. 0, XFS_TRANS_PERM_LOG_RES,
  797. XFS_ITRUNCATE_LOG_COUNT);
  798. if (error) {
  799. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  800. xfs_trans_cancel(tp, 0);
  801. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  802. return error;
  803. }
  804. xfs_ilock(ip, XFS_ILOCK_EXCL);
  805. xfs_trans_ijoin(tp, ip,
  806. XFS_IOLOCK_EXCL |
  807. XFS_ILOCK_EXCL);
  808. xfs_trans_ihold(tp, ip);
  809. error = xfs_itruncate_finish(&tp, ip,
  810. ip->i_size,
  811. XFS_DATA_FORK,
  812. 0);
  813. /*
  814. * If we get an error at this point we
  815. * simply don't bother truncating the file.
  816. */
  817. if (error) {
  818. xfs_trans_cancel(tp,
  819. (XFS_TRANS_RELEASE_LOG_RES |
  820. XFS_TRANS_ABORT));
  821. } else {
  822. error = xfs_trans_commit(tp,
  823. XFS_TRANS_RELEASE_LOG_RES);
  824. }
  825. xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
  826. : XFS_ILOCK_EXCL));
  827. }
  828. return error;
  829. }
  830. /*
  831. * Free a symlink that has blocks associated with it.
  832. */
  833. STATIC int
  834. xfs_inactive_symlink_rmt(
  835. xfs_inode_t *ip,
  836. xfs_trans_t **tpp)
  837. {
  838. xfs_buf_t *bp;
  839. int committed;
  840. int done;
  841. int error;
  842. xfs_fsblock_t first_block;
  843. xfs_bmap_free_t free_list;
  844. int i;
  845. xfs_mount_t *mp;
  846. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  847. int nmaps;
  848. xfs_trans_t *ntp;
  849. int size;
  850. xfs_trans_t *tp;
  851. tp = *tpp;
  852. mp = ip->i_mount;
  853. ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
  854. /*
  855. * We're freeing a symlink that has some
  856. * blocks allocated to it. Free the
  857. * blocks here. We know that we've got
  858. * either 1 or 2 extents and that we can
  859. * free them all in one bunmapi call.
  860. */
  861. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  862. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  863. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  864. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  865. xfs_trans_cancel(tp, 0);
  866. *tpp = NULL;
  867. return error;
  868. }
  869. /*
  870. * Lock the inode, fix the size, and join it to the transaction.
  871. * Hold it so in the normal path, we still have it locked for
  872. * the second transaction. In the error paths we need it
  873. * held so the cancel won't rele it, see below.
  874. */
  875. xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  876. size = (int)ip->i_d.di_size;
  877. ip->i_d.di_size = 0;
  878. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  879. xfs_trans_ihold(tp, ip);
  880. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  881. /*
  882. * Find the block(s) so we can inval and unmap them.
  883. */
  884. done = 0;
  885. XFS_BMAP_INIT(&free_list, &first_block);
  886. nmaps = ARRAY_SIZE(mval);
  887. if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
  888. XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
  889. &free_list, NULL)))
  890. goto error0;
  891. /*
  892. * Invalidate the block(s).
  893. */
  894. for (i = 0; i < nmaps; i++) {
  895. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  896. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  897. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  898. xfs_trans_binval(tp, bp);
  899. }
  900. /*
  901. * Unmap the dead block(s) to the free_list.
  902. */
  903. if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
  904. &first_block, &free_list, NULL, &done)))
  905. goto error1;
  906. ASSERT(done);
  907. /*
  908. * Commit the first transaction. This logs the EFI and the inode.
  909. */
  910. if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
  911. goto error1;
  912. /*
  913. * The transaction must have been committed, since there were
  914. * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
  915. * The new tp has the extent freeing and EFDs.
  916. */
  917. ASSERT(committed);
  918. /*
  919. * The first xact was committed, so add the inode to the new one.
  920. * Mark it dirty so it will be logged and moved forward in the log as
  921. * part of every commit.
  922. */
  923. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  924. xfs_trans_ihold(tp, ip);
  925. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  926. /*
  927. * Get a new, empty transaction to return to our caller.
  928. */
  929. ntp = xfs_trans_dup(tp);
  930. /*
  931. * Commit the transaction containing extent freeing and EFDs.
  932. * If we get an error on the commit here or on the reserve below,
  933. * we need to unlock the inode since the new transaction doesn't
  934. * have the inode attached.
  935. */
  936. error = xfs_trans_commit(tp, 0);
  937. tp = ntp;
  938. if (error) {
  939. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  940. goto error0;
  941. }
  942. /*
  943. * Remove the memory for extent descriptions (just bookkeeping).
  944. */
  945. if (ip->i_df.if_bytes)
  946. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  947. ASSERT(ip->i_df.if_bytes == 0);
  948. /*
  949. * Put an itruncate log reservation in the new transaction
  950. * for our caller.
  951. */
  952. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  953. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  954. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  955. goto error0;
  956. }
  957. /*
  958. * Return with the inode locked but not joined to the transaction.
  959. */
  960. *tpp = tp;
  961. return 0;
  962. error1:
  963. xfs_bmap_cancel(&free_list);
  964. error0:
  965. /*
  966. * Have to come here with the inode locked and either
  967. * (held and in the transaction) or (not in the transaction).
  968. * If the inode isn't held then cancel would iput it, but
  969. * that's wrong since this is inactive and the vnode ref
  970. * count is 0 already.
  971. * Cancel won't do anything to the inode if held, but it still
  972. * needs to be locked until the cancel is done, if it was
  973. * joined to the transaction.
  974. */
  975. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  976. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  977. *tpp = NULL;
  978. return error;
  979. }
  980. STATIC int
  981. xfs_inactive_symlink_local(
  982. xfs_inode_t *ip,
  983. xfs_trans_t **tpp)
  984. {
  985. int error;
  986. ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
  987. /*
  988. * We're freeing a symlink which fit into
  989. * the inode. Just free the memory used
  990. * to hold the old symlink.
  991. */
  992. error = xfs_trans_reserve(*tpp, 0,
  993. XFS_ITRUNCATE_LOG_RES(ip->i_mount),
  994. 0, XFS_TRANS_PERM_LOG_RES,
  995. XFS_ITRUNCATE_LOG_COUNT);
  996. if (error) {
  997. xfs_trans_cancel(*tpp, 0);
  998. *tpp = NULL;
  999. return error;
  1000. }
  1001. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1002. /*
  1003. * Zero length symlinks _can_ exist.
  1004. */
  1005. if (ip->i_df.if_bytes > 0) {
  1006. xfs_idata_realloc(ip,
  1007. -(ip->i_df.if_bytes),
  1008. XFS_DATA_FORK);
  1009. ASSERT(ip->i_df.if_bytes == 0);
  1010. }
  1011. return 0;
  1012. }
  1013. STATIC int
  1014. xfs_inactive_attrs(
  1015. xfs_inode_t *ip,
  1016. xfs_trans_t **tpp)
  1017. {
  1018. xfs_trans_t *tp;
  1019. int error;
  1020. xfs_mount_t *mp;
  1021. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1022. tp = *tpp;
  1023. mp = ip->i_mount;
  1024. ASSERT(ip->i_d.di_forkoff != 0);
  1025. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1026. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1027. if (error)
  1028. goto error_unlock;
  1029. error = xfs_attr_inactive(ip);
  1030. if (error)
  1031. goto error_unlock;
  1032. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1033. error = xfs_trans_reserve(tp, 0,
  1034. XFS_IFREE_LOG_RES(mp),
  1035. 0, XFS_TRANS_PERM_LOG_RES,
  1036. XFS_INACTIVE_LOG_COUNT);
  1037. if (error)
  1038. goto error_cancel;
  1039. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1040. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1041. xfs_trans_ihold(tp, ip);
  1042. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1043. ASSERT(ip->i_d.di_anextents == 0);
  1044. *tpp = tp;
  1045. return 0;
  1046. error_cancel:
  1047. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1048. xfs_trans_cancel(tp, 0);
  1049. error_unlock:
  1050. *tpp = NULL;
  1051. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1052. return error;
  1053. }
  1054. int
  1055. xfs_release(
  1056. xfs_inode_t *ip)
  1057. {
  1058. xfs_mount_t *mp = ip->i_mount;
  1059. int error;
  1060. if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
  1061. return 0;
  1062. /* If this is a read-only mount, don't do this (would generate I/O) */
  1063. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1064. return 0;
  1065. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1066. int truncated;
  1067. /*
  1068. * If we are using filestreams, and we have an unlinked
  1069. * file that we are processing the last close on, then nothing
  1070. * will be able to reopen and write to this file. Purge this
  1071. * inode from the filestreams cache so that it doesn't delay
  1072. * teardown of the inode.
  1073. */
  1074. if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
  1075. xfs_filestream_deassociate(ip);
  1076. /*
  1077. * If we previously truncated this file and removed old data
  1078. * in the process, we want to initiate "early" writeout on
  1079. * the last close. This is an attempt to combat the notorious
  1080. * NULL files problem which is particularly noticable from a
  1081. * truncate down, buffered (re-)write (delalloc), followed by
  1082. * a crash. What we are effectively doing here is
  1083. * significantly reducing the time window where we'd otherwise
  1084. * be exposed to that problem.
  1085. */
  1086. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  1087. if (truncated && VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
  1088. xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
  1089. }
  1090. if (ip->i_d.di_nlink != 0) {
  1091. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1092. ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
  1093. ip->i_delayed_blks > 0)) &&
  1094. (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
  1095. (!(ip->i_d.di_flags &
  1096. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
  1097. error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
  1098. if (error)
  1099. return error;
  1100. }
  1101. }
  1102. return 0;
  1103. }
  1104. /*
  1105. * xfs_inactive
  1106. *
  1107. * This is called when the vnode reference count for the vnode
  1108. * goes to zero. If the file has been unlinked, then it must
  1109. * now be truncated. Also, we clear all of the read-ahead state
  1110. * kept for the inode here since the file is now closed.
  1111. */
  1112. int
  1113. xfs_inactive(
  1114. xfs_inode_t *ip)
  1115. {
  1116. xfs_bmap_free_t free_list;
  1117. xfs_fsblock_t first_block;
  1118. int committed;
  1119. xfs_trans_t *tp;
  1120. xfs_mount_t *mp;
  1121. int error;
  1122. int truncate;
  1123. xfs_itrace_entry(ip);
  1124. /*
  1125. * If the inode is already free, then there can be nothing
  1126. * to clean up here.
  1127. */
  1128. if (ip->i_d.di_mode == 0 || VN_BAD(VFS_I(ip))) {
  1129. ASSERT(ip->i_df.if_real_bytes == 0);
  1130. ASSERT(ip->i_df.if_broot_bytes == 0);
  1131. return VN_INACTIVE_CACHE;
  1132. }
  1133. /*
  1134. * Only do a truncate if it's a regular file with
  1135. * some actual space in it. It's OK to look at the
  1136. * inode's fields without the lock because we're the
  1137. * only one with a reference to the inode.
  1138. */
  1139. truncate = ((ip->i_d.di_nlink == 0) &&
  1140. ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
  1141. (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
  1142. ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
  1143. mp = ip->i_mount;
  1144. if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
  1145. XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
  1146. error = 0;
  1147. /* If this is a read-only mount, don't do this (would generate I/O) */
  1148. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1149. goto out;
  1150. if (ip->i_d.di_nlink != 0) {
  1151. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1152. ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
  1153. ip->i_delayed_blks > 0)) &&
  1154. (ip->i_df.if_flags & XFS_IFEXTENTS) &&
  1155. (!(ip->i_d.di_flags &
  1156. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
  1157. (ip->i_delayed_blks != 0)))) {
  1158. error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
  1159. if (error)
  1160. return VN_INACTIVE_CACHE;
  1161. }
  1162. goto out;
  1163. }
  1164. ASSERT(ip->i_d.di_nlink == 0);
  1165. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  1166. return VN_INACTIVE_CACHE;
  1167. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1168. if (truncate) {
  1169. /*
  1170. * Do the xfs_itruncate_start() call before
  1171. * reserving any log space because itruncate_start
  1172. * will call into the buffer cache and we can't
  1173. * do that within a transaction.
  1174. */
  1175. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1176. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
  1177. if (error) {
  1178. xfs_trans_cancel(tp, 0);
  1179. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1180. return VN_INACTIVE_CACHE;
  1181. }
  1182. error = xfs_trans_reserve(tp, 0,
  1183. XFS_ITRUNCATE_LOG_RES(mp),
  1184. 0, XFS_TRANS_PERM_LOG_RES,
  1185. XFS_ITRUNCATE_LOG_COUNT);
  1186. if (error) {
  1187. /* Don't call itruncate_cleanup */
  1188. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1189. xfs_trans_cancel(tp, 0);
  1190. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1191. return VN_INACTIVE_CACHE;
  1192. }
  1193. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1194. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1195. xfs_trans_ihold(tp, ip);
  1196. /*
  1197. * normally, we have to run xfs_itruncate_finish sync.
  1198. * But if filesystem is wsync and we're in the inactive
  1199. * path, then we know that nlink == 0, and that the
  1200. * xaction that made nlink == 0 is permanently committed
  1201. * since xfs_remove runs as a synchronous transaction.
  1202. */
  1203. error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
  1204. (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
  1205. if (error) {
  1206. xfs_trans_cancel(tp,
  1207. XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1208. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1209. return VN_INACTIVE_CACHE;
  1210. }
  1211. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
  1212. /*
  1213. * If we get an error while cleaning up a
  1214. * symlink we bail out.
  1215. */
  1216. error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
  1217. xfs_inactive_symlink_rmt(ip, &tp) :
  1218. xfs_inactive_symlink_local(ip, &tp);
  1219. if (error) {
  1220. ASSERT(tp == NULL);
  1221. return VN_INACTIVE_CACHE;
  1222. }
  1223. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1224. xfs_trans_ihold(tp, ip);
  1225. } else {
  1226. error = xfs_trans_reserve(tp, 0,
  1227. XFS_IFREE_LOG_RES(mp),
  1228. 0, XFS_TRANS_PERM_LOG_RES,
  1229. XFS_INACTIVE_LOG_COUNT);
  1230. if (error) {
  1231. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1232. xfs_trans_cancel(tp, 0);
  1233. return VN_INACTIVE_CACHE;
  1234. }
  1235. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1236. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1237. xfs_trans_ihold(tp, ip);
  1238. }
  1239. /*
  1240. * If there are attributes associated with the file
  1241. * then blow them away now. The code calls a routine
  1242. * that recursively deconstructs the attribute fork.
  1243. * We need to just commit the current transaction
  1244. * because we can't use it for xfs_attr_inactive().
  1245. */
  1246. if (ip->i_d.di_anextents > 0) {
  1247. error = xfs_inactive_attrs(ip, &tp);
  1248. /*
  1249. * If we got an error, the transaction is already
  1250. * cancelled, and the inode is unlocked. Just get out.
  1251. */
  1252. if (error)
  1253. return VN_INACTIVE_CACHE;
  1254. } else if (ip->i_afp) {
  1255. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1256. }
  1257. /*
  1258. * Free the inode.
  1259. */
  1260. XFS_BMAP_INIT(&free_list, &first_block);
  1261. error = xfs_ifree(tp, ip, &free_list);
  1262. if (error) {
  1263. /*
  1264. * If we fail to free the inode, shut down. The cancel
  1265. * might do that, we need to make sure. Otherwise the
  1266. * inode might be lost for a long time or forever.
  1267. */
  1268. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1269. cmn_err(CE_NOTE,
  1270. "xfs_inactive: xfs_ifree() returned an error = %d on %s",
  1271. error, mp->m_fsname);
  1272. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1273. }
  1274. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  1275. } else {
  1276. /*
  1277. * Credit the quota account(s). The inode is gone.
  1278. */
  1279. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1280. /*
  1281. * Just ignore errors at this point. There is nothing we can
  1282. * do except to try to keep going. Make sure it's not a silent
  1283. * error.
  1284. */
  1285. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1286. if (error)
  1287. xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
  1288. "xfs_bmap_finish() returned error %d", error);
  1289. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1290. if (error)
  1291. xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
  1292. "xfs_trans_commit() returned error %d", error);
  1293. }
  1294. /*
  1295. * Release the dquots held by inode, if any.
  1296. */
  1297. XFS_QM_DQDETACH(mp, ip);
  1298. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1299. out:
  1300. return VN_INACTIVE_CACHE;
  1301. }
  1302. /*
  1303. * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
  1304. * is allowed, otherwise it has to be an exact match. If a CI match is found,
  1305. * ci_name->name will point to a the actual name (caller must free) or
  1306. * will be set to NULL if an exact match is found.
  1307. */
  1308. int
  1309. xfs_lookup(
  1310. xfs_inode_t *dp,
  1311. struct xfs_name *name,
  1312. xfs_inode_t **ipp,
  1313. struct xfs_name *ci_name)
  1314. {
  1315. xfs_ino_t inum;
  1316. int error;
  1317. uint lock_mode;
  1318. xfs_itrace_entry(dp);
  1319. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  1320. return XFS_ERROR(EIO);
  1321. lock_mode = xfs_ilock_map_shared(dp);
  1322. error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
  1323. xfs_iunlock_map_shared(dp, lock_mode);
  1324. if (error)
  1325. goto out;
  1326. error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp, 0);
  1327. if (error)
  1328. goto out_free_name;
  1329. xfs_itrace_ref(*ipp);
  1330. return 0;
  1331. out_free_name:
  1332. if (ci_name)
  1333. kmem_free(ci_name->name);
  1334. out:
  1335. *ipp = NULL;
  1336. return error;
  1337. }
  1338. int
  1339. xfs_create(
  1340. xfs_inode_t *dp,
  1341. struct xfs_name *name,
  1342. mode_t mode,
  1343. xfs_dev_t rdev,
  1344. xfs_inode_t **ipp,
  1345. cred_t *credp)
  1346. {
  1347. xfs_mount_t *mp = dp->i_mount;
  1348. xfs_inode_t *ip;
  1349. xfs_trans_t *tp;
  1350. int error;
  1351. xfs_bmap_free_t free_list;
  1352. xfs_fsblock_t first_block;
  1353. boolean_t unlock_dp_on_error = B_FALSE;
  1354. int dm_event_sent = 0;
  1355. uint cancel_flags;
  1356. int committed;
  1357. xfs_prid_t prid;
  1358. struct xfs_dquot *udqp, *gdqp;
  1359. uint resblks;
  1360. ASSERT(!*ipp);
  1361. xfs_itrace_entry(dp);
  1362. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  1363. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  1364. dp, DM_RIGHT_NULL, NULL,
  1365. DM_RIGHT_NULL, name->name, NULL,
  1366. mode, 0, 0);
  1367. if (error)
  1368. return error;
  1369. dm_event_sent = 1;
  1370. }
  1371. if (XFS_FORCED_SHUTDOWN(mp))
  1372. return XFS_ERROR(EIO);
  1373. /* Return through std_return after this point. */
  1374. udqp = gdqp = NULL;
  1375. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1376. prid = dp->i_d.di_projid;
  1377. else
  1378. prid = (xfs_prid_t)dfltprid;
  1379. /*
  1380. * Make sure that we have allocated dquot(s) on disk.
  1381. */
  1382. error = XFS_QM_DQVOPALLOC(mp, dp,
  1383. current_fsuid(credp), current_fsgid(credp), prid,
  1384. XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
  1385. if (error)
  1386. goto std_return;
  1387. ip = NULL;
  1388. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1389. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1390. resblks = XFS_CREATE_SPACE_RES(mp, name->len);
  1391. /*
  1392. * Initially assume that the file does not exist and
  1393. * reserve the resources for that case. If that is not
  1394. * the case we'll drop the one we have and get a more
  1395. * appropriate transaction later.
  1396. */
  1397. error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
  1398. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1399. if (error == ENOSPC) {
  1400. resblks = 0;
  1401. error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
  1402. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1403. }
  1404. if (error) {
  1405. cancel_flags = 0;
  1406. goto error_return;
  1407. }
  1408. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  1409. unlock_dp_on_error = B_TRUE;
  1410. XFS_BMAP_INIT(&free_list, &first_block);
  1411. ASSERT(ip == NULL);
  1412. /*
  1413. * Reserve disk quota and the inode.
  1414. */
  1415. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  1416. if (error)
  1417. goto error_return;
  1418. error = xfs_dir_canenter(tp, dp, name, resblks);
  1419. if (error)
  1420. goto error_return;
  1421. error = xfs_dir_ialloc(&tp, dp, mode, 1,
  1422. rdev, credp, prid, resblks > 0,
  1423. &ip, &committed);
  1424. if (error) {
  1425. if (error == ENOSPC)
  1426. goto error_return;
  1427. goto abort_return;
  1428. }
  1429. xfs_itrace_ref(ip);
  1430. /*
  1431. * At this point, we've gotten a newly allocated inode.
  1432. * It is locked (and joined to the transaction).
  1433. */
  1434. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1435. /*
  1436. * Now we join the directory inode to the transaction. We do not do it
  1437. * earlier because xfs_dir_ialloc might commit the previous transaction
  1438. * (and release all the locks). An error from here on will result in
  1439. * the transaction cancel unlocking dp so don't do it explicitly in the
  1440. * error path.
  1441. */
  1442. IHOLD(dp);
  1443. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1444. unlock_dp_on_error = B_FALSE;
  1445. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  1446. &first_block, &free_list, resblks ?
  1447. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1448. if (error) {
  1449. ASSERT(error != ENOSPC);
  1450. goto abort_return;
  1451. }
  1452. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1453. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1454. /*
  1455. * If this is a synchronous mount, make sure that the
  1456. * create transaction goes to disk before returning to
  1457. * the user.
  1458. */
  1459. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1460. xfs_trans_set_sync(tp);
  1461. }
  1462. dp->i_gen++;
  1463. /*
  1464. * Attach the dquot(s) to the inodes and modify them incore.
  1465. * These ids of the inode couldn't have changed since the new
  1466. * inode has been locked ever since it was created.
  1467. */
  1468. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  1469. /*
  1470. * xfs_trans_commit normally decrements the vnode ref count
  1471. * when it unlocks the inode. Since we want to return the
  1472. * vnode to the caller, we bump the vnode ref count now.
  1473. */
  1474. IHOLD(ip);
  1475. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1476. if (error) {
  1477. xfs_bmap_cancel(&free_list);
  1478. goto abort_rele;
  1479. }
  1480. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1481. if (error) {
  1482. IRELE(ip);
  1483. tp = NULL;
  1484. goto error_return;
  1485. }
  1486. XFS_QM_DQRELE(mp, udqp);
  1487. XFS_QM_DQRELE(mp, gdqp);
  1488. *ipp = ip;
  1489. /* Fallthrough to std_return with error = 0 */
  1490. std_return:
  1491. if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
  1492. DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  1493. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  1494. dp, DM_RIGHT_NULL,
  1495. *ipp ? ip : NULL,
  1496. DM_RIGHT_NULL, name->name, NULL,
  1497. mode, error, 0);
  1498. }
  1499. return error;
  1500. abort_return:
  1501. cancel_flags |= XFS_TRANS_ABORT;
  1502. /* FALLTHROUGH */
  1503. error_return:
  1504. if (tp != NULL)
  1505. xfs_trans_cancel(tp, cancel_flags);
  1506. XFS_QM_DQRELE(mp, udqp);
  1507. XFS_QM_DQRELE(mp, gdqp);
  1508. if (unlock_dp_on_error)
  1509. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1510. goto std_return;
  1511. abort_rele:
  1512. /*
  1513. * Wait until after the current transaction is aborted to
  1514. * release the inode. This prevents recursive transactions
  1515. * and deadlocks from xfs_inactive.
  1516. */
  1517. cancel_flags |= XFS_TRANS_ABORT;
  1518. xfs_trans_cancel(tp, cancel_flags);
  1519. IRELE(ip);
  1520. XFS_QM_DQRELE(mp, udqp);
  1521. XFS_QM_DQRELE(mp, gdqp);
  1522. goto std_return;
  1523. }
  1524. #ifdef DEBUG
  1525. int xfs_locked_n;
  1526. int xfs_small_retries;
  1527. int xfs_middle_retries;
  1528. int xfs_lots_retries;
  1529. int xfs_lock_delays;
  1530. #endif
  1531. /*
  1532. * Bump the subclass so xfs_lock_inodes() acquires each lock with
  1533. * a different value
  1534. */
  1535. static inline int
  1536. xfs_lock_inumorder(int lock_mode, int subclass)
  1537. {
  1538. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  1539. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  1540. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  1541. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  1542. return lock_mode;
  1543. }
  1544. /*
  1545. * The following routine will lock n inodes in exclusive mode.
  1546. * We assume the caller calls us with the inodes in i_ino order.
  1547. *
  1548. * We need to detect deadlock where an inode that we lock
  1549. * is in the AIL and we start waiting for another inode that is locked
  1550. * by a thread in a long running transaction (such as truncate). This can
  1551. * result in deadlock since the long running trans might need to wait
  1552. * for the inode we just locked in order to push the tail and free space
  1553. * in the log.
  1554. */
  1555. void
  1556. xfs_lock_inodes(
  1557. xfs_inode_t **ips,
  1558. int inodes,
  1559. uint lock_mode)
  1560. {
  1561. int attempts = 0, i, j, try_lock;
  1562. xfs_log_item_t *lp;
  1563. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  1564. try_lock = 0;
  1565. i = 0;
  1566. again:
  1567. for (; i < inodes; i++) {
  1568. ASSERT(ips[i]);
  1569. if (i && (ips[i] == ips[i-1])) /* Already locked */
  1570. continue;
  1571. /*
  1572. * If try_lock is not set yet, make sure all locked inodes
  1573. * are not in the AIL.
  1574. * If any are, set try_lock to be used later.
  1575. */
  1576. if (!try_lock) {
  1577. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  1578. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  1579. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1580. try_lock++;
  1581. }
  1582. }
  1583. }
  1584. /*
  1585. * If any of the previous locks we have locked is in the AIL,
  1586. * we must TRY to get the second and subsequent locks. If
  1587. * we can't get any, we must release all we have
  1588. * and try again.
  1589. */
  1590. if (try_lock) {
  1591. /* try_lock must be 0 if i is 0. */
  1592. /*
  1593. * try_lock means we have an inode locked
  1594. * that is in the AIL.
  1595. */
  1596. ASSERT(i != 0);
  1597. if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
  1598. attempts++;
  1599. /*
  1600. * Unlock all previous guys and try again.
  1601. * xfs_iunlock will try to push the tail
  1602. * if the inode is in the AIL.
  1603. */
  1604. for(j = i - 1; j >= 0; j--) {
  1605. /*
  1606. * Check to see if we've already
  1607. * unlocked this one.
  1608. * Not the first one going back,
  1609. * and the inode ptr is the same.
  1610. */
  1611. if ((j != (i - 1)) && ips[j] ==
  1612. ips[j+1])
  1613. continue;
  1614. xfs_iunlock(ips[j], lock_mode);
  1615. }
  1616. if ((attempts % 5) == 0) {
  1617. delay(1); /* Don't just spin the CPU */
  1618. #ifdef DEBUG
  1619. xfs_lock_delays++;
  1620. #endif
  1621. }
  1622. i = 0;
  1623. try_lock = 0;
  1624. goto again;
  1625. }
  1626. } else {
  1627. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  1628. }
  1629. }
  1630. #ifdef DEBUG
  1631. if (attempts) {
  1632. if (attempts < 5) xfs_small_retries++;
  1633. else if (attempts < 100) xfs_middle_retries++;
  1634. else xfs_lots_retries++;
  1635. } else {
  1636. xfs_locked_n++;
  1637. }
  1638. #endif
  1639. }
  1640. void
  1641. xfs_lock_two_inodes(
  1642. xfs_inode_t *ip0,
  1643. xfs_inode_t *ip1,
  1644. uint lock_mode)
  1645. {
  1646. xfs_inode_t *temp;
  1647. int attempts = 0;
  1648. xfs_log_item_t *lp;
  1649. ASSERT(ip0->i_ino != ip1->i_ino);
  1650. if (ip0->i_ino > ip1->i_ino) {
  1651. temp = ip0;
  1652. ip0 = ip1;
  1653. ip1 = temp;
  1654. }
  1655. again:
  1656. xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
  1657. /*
  1658. * If the first lock we have locked is in the AIL, we must TRY to get
  1659. * the second lock. If we can't get it, we must release the first one
  1660. * and try again.
  1661. */
  1662. lp = (xfs_log_item_t *)ip0->i_itemp;
  1663. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1664. if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
  1665. xfs_iunlock(ip0, lock_mode);
  1666. if ((++attempts % 5) == 0)
  1667. delay(1); /* Don't just spin the CPU */
  1668. goto again;
  1669. }
  1670. } else {
  1671. xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
  1672. }
  1673. }
  1674. int
  1675. xfs_remove(
  1676. xfs_inode_t *dp,
  1677. struct xfs_name *name,
  1678. xfs_inode_t *ip)
  1679. {
  1680. xfs_mount_t *mp = dp->i_mount;
  1681. xfs_trans_t *tp = NULL;
  1682. int is_dir = S_ISDIR(ip->i_d.di_mode);
  1683. int error = 0;
  1684. xfs_bmap_free_t free_list;
  1685. xfs_fsblock_t first_block;
  1686. int cancel_flags;
  1687. int committed;
  1688. int link_zero;
  1689. uint resblks;
  1690. uint log_count;
  1691. xfs_itrace_entry(dp);
  1692. xfs_itrace_entry(ip);
  1693. if (XFS_FORCED_SHUTDOWN(mp))
  1694. return XFS_ERROR(EIO);
  1695. if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
  1696. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
  1697. NULL, DM_RIGHT_NULL, name->name, NULL,
  1698. ip->i_d.di_mode, 0, 0);
  1699. if (error)
  1700. return error;
  1701. }
  1702. error = XFS_QM_DQATTACH(mp, dp, 0);
  1703. if (error)
  1704. goto std_return;
  1705. error = XFS_QM_DQATTACH(mp, ip, 0);
  1706. if (error)
  1707. goto std_return;
  1708. if (is_dir) {
  1709. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  1710. log_count = XFS_DEFAULT_LOG_COUNT;
  1711. } else {
  1712. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  1713. log_count = XFS_REMOVE_LOG_COUNT;
  1714. }
  1715. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1716. /*
  1717. * We try to get the real space reservation first,
  1718. * allowing for directory btree deletion(s) implying
  1719. * possible bmap insert(s). If we can't get the space
  1720. * reservation then we use 0 instead, and avoid the bmap
  1721. * btree insert(s) in the directory code by, if the bmap
  1722. * insert tries to happen, instead trimming the LAST
  1723. * block from the directory.
  1724. */
  1725. resblks = XFS_REMOVE_SPACE_RES(mp);
  1726. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  1727. XFS_TRANS_PERM_LOG_RES, log_count);
  1728. if (error == ENOSPC) {
  1729. resblks = 0;
  1730. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  1731. XFS_TRANS_PERM_LOG_RES, log_count);
  1732. }
  1733. if (error) {
  1734. ASSERT(error != ENOSPC);
  1735. cancel_flags = 0;
  1736. goto out_trans_cancel;
  1737. }
  1738. xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
  1739. /*
  1740. * At this point, we've gotten both the directory and the entry
  1741. * inodes locked.
  1742. */
  1743. IHOLD(ip);
  1744. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1745. IHOLD(dp);
  1746. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1747. /*
  1748. * If we're removing a directory perform some additional validation.
  1749. */
  1750. if (is_dir) {
  1751. ASSERT(ip->i_d.di_nlink >= 2);
  1752. if (ip->i_d.di_nlink != 2) {
  1753. error = XFS_ERROR(ENOTEMPTY);
  1754. goto out_trans_cancel;
  1755. }
  1756. if (!xfs_dir_isempty(ip)) {
  1757. error = XFS_ERROR(ENOTEMPTY);
  1758. goto out_trans_cancel;
  1759. }
  1760. }
  1761. XFS_BMAP_INIT(&free_list, &first_block);
  1762. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  1763. &first_block, &free_list, resblks);
  1764. if (error) {
  1765. ASSERT(error != ENOENT);
  1766. goto out_bmap_cancel;
  1767. }
  1768. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1769. /*
  1770. * Bump the in memory generation count on the parent
  1771. * directory so that other can know that it has changed.
  1772. */
  1773. dp->i_gen++;
  1774. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1775. if (is_dir) {
  1776. /*
  1777. * Drop the link from ip's "..".
  1778. */
  1779. error = xfs_droplink(tp, dp);
  1780. if (error)
  1781. goto out_bmap_cancel;
  1782. /*
  1783. * Drop the link from dp to ip.
  1784. */
  1785. error = xfs_droplink(tp, ip);
  1786. if (error)
  1787. goto out_bmap_cancel;
  1788. } else {
  1789. /*
  1790. * When removing a non-directory we need to log the parent
  1791. * inode here for the i_gen update. For a directory this is
  1792. * done implicitly by the xfs_droplink call for the ".." entry.
  1793. */
  1794. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1795. }
  1796. /*
  1797. * Drop the "." link from ip to self.
  1798. */
  1799. error = xfs_droplink(tp, ip);
  1800. if (error)
  1801. goto out_bmap_cancel;
  1802. /*
  1803. * Determine if this is the last link while
  1804. * we are in the transaction.
  1805. */
  1806. link_zero = (ip->i_d.di_nlink == 0);
  1807. /*
  1808. * If this is a synchronous mount, make sure that the
  1809. * remove transaction goes to disk before returning to
  1810. * the user.
  1811. */
  1812. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  1813. xfs_trans_set_sync(tp);
  1814. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1815. if (error)
  1816. goto out_bmap_cancel;
  1817. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1818. if (error)
  1819. goto std_return;
  1820. /*
  1821. * If we are using filestreams, kill the stream association.
  1822. * If the file is still open it may get a new one but that
  1823. * will get killed on last close in xfs_close() so we don't
  1824. * have to worry about that.
  1825. */
  1826. if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
  1827. xfs_filestream_deassociate(ip);
  1828. xfs_itrace_exit(ip);
  1829. xfs_itrace_exit(dp);
  1830. std_return:
  1831. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
  1832. XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE, dp, DM_RIGHT_NULL,
  1833. NULL, DM_RIGHT_NULL, name->name, NULL,
  1834. ip->i_d.di_mode, error, 0);
  1835. }
  1836. return error;
  1837. out_bmap_cancel:
  1838. xfs_bmap_cancel(&free_list);
  1839. cancel_flags |= XFS_TRANS_ABORT;
  1840. out_trans_cancel:
  1841. xfs_trans_cancel(tp, cancel_flags);
  1842. goto std_return;
  1843. }
  1844. int
  1845. xfs_link(
  1846. xfs_inode_t *tdp,
  1847. xfs_inode_t *sip,
  1848. struct xfs_name *target_name)
  1849. {
  1850. xfs_mount_t *mp = tdp->i_mount;
  1851. xfs_trans_t *tp;
  1852. int error;
  1853. xfs_bmap_free_t free_list;
  1854. xfs_fsblock_t first_block;
  1855. int cancel_flags;
  1856. int committed;
  1857. int resblks;
  1858. xfs_itrace_entry(tdp);
  1859. xfs_itrace_entry(sip);
  1860. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  1861. if (XFS_FORCED_SHUTDOWN(mp))
  1862. return XFS_ERROR(EIO);
  1863. if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
  1864. error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
  1865. tdp, DM_RIGHT_NULL,
  1866. sip, DM_RIGHT_NULL,
  1867. target_name->name, NULL, 0, 0, 0);
  1868. if (error)
  1869. return error;
  1870. }
  1871. /* Return through std_return after this point. */
  1872. error = XFS_QM_DQATTACH(mp, sip, 0);
  1873. if (!error && sip != tdp)
  1874. error = XFS_QM_DQATTACH(mp, tdp, 0);
  1875. if (error)
  1876. goto std_return;
  1877. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  1878. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1879. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  1880. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  1881. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  1882. if (error == ENOSPC) {
  1883. resblks = 0;
  1884. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  1885. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  1886. }
  1887. if (error) {
  1888. cancel_flags = 0;
  1889. goto error_return;
  1890. }
  1891. xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
  1892. /*
  1893. * Increment vnode ref counts since xfs_trans_commit &
  1894. * xfs_trans_cancel will both unlock the inodes and
  1895. * decrement the associated ref counts.
  1896. */
  1897. IHOLD(sip);
  1898. IHOLD(tdp);
  1899. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  1900. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  1901. /*
  1902. * If the source has too many links, we can't make any more to it.
  1903. */
  1904. if (sip->i_d.di_nlink >= XFS_MAXLINK) {
  1905. error = XFS_ERROR(EMLINK);
  1906. goto error_return;
  1907. }
  1908. /*
  1909. * If we are using project inheritance, we only allow hard link
  1910. * creation in our tree when the project IDs are the same; else
  1911. * the tree quota mechanism could be circumvented.
  1912. */
  1913. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  1914. (tdp->i_d.di_projid != sip->i_d.di_projid))) {
  1915. error = XFS_ERROR(EXDEV);
  1916. goto error_return;
  1917. }
  1918. error = xfs_dir_canenter(tp, tdp, target_name, resblks);
  1919. if (error)
  1920. goto error_return;
  1921. XFS_BMAP_INIT(&free_list, &first_block);
  1922. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  1923. &first_block, &free_list, resblks);
  1924. if (error)
  1925. goto abort_return;
  1926. xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1927. tdp->i_gen++;
  1928. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  1929. error = xfs_bumplink(tp, sip);
  1930. if (error)
  1931. goto abort_return;
  1932. /*
  1933. * If this is a synchronous mount, make sure that the
  1934. * link transaction goes to disk before returning to
  1935. * the user.
  1936. */
  1937. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1938. xfs_trans_set_sync(tp);
  1939. }
  1940. error = xfs_bmap_finish (&tp, &free_list, &committed);
  1941. if (error) {
  1942. xfs_bmap_cancel(&free_list);
  1943. goto abort_return;
  1944. }
  1945. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1946. if (error)
  1947. goto std_return;
  1948. /* Fall through to std_return with error = 0. */
  1949. std_return:
  1950. if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
  1951. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
  1952. tdp, DM_RIGHT_NULL,
  1953. sip, DM_RIGHT_NULL,
  1954. target_name->name, NULL, 0, error, 0);
  1955. }
  1956. return error;
  1957. abort_return:
  1958. cancel_flags |= XFS_TRANS_ABORT;
  1959. /* FALLTHROUGH */
  1960. error_return:
  1961. xfs_trans_cancel(tp, cancel_flags);
  1962. goto std_return;
  1963. }
  1964. int
  1965. xfs_mkdir(
  1966. xfs_inode_t *dp,
  1967. struct xfs_name *dir_name,
  1968. mode_t mode,
  1969. xfs_inode_t **ipp,
  1970. cred_t *credp)
  1971. {
  1972. xfs_mount_t *mp = dp->i_mount;
  1973. xfs_inode_t *cdp; /* inode of created dir */
  1974. xfs_trans_t *tp;
  1975. int cancel_flags;
  1976. int error;
  1977. int committed;
  1978. xfs_bmap_free_t free_list;
  1979. xfs_fsblock_t first_block;
  1980. boolean_t unlock_dp_on_error = B_FALSE;
  1981. boolean_t created = B_FALSE;
  1982. int dm_event_sent = 0;
  1983. xfs_prid_t prid;
  1984. struct xfs_dquot *udqp, *gdqp;
  1985. uint resblks;
  1986. if (XFS_FORCED_SHUTDOWN(mp))
  1987. return XFS_ERROR(EIO);
  1988. tp = NULL;
  1989. if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
  1990. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  1991. dp, DM_RIGHT_NULL, NULL,
  1992. DM_RIGHT_NULL, dir_name->name, NULL,
  1993. mode, 0, 0);
  1994. if (error)
  1995. return error;
  1996. dm_event_sent = 1;
  1997. }
  1998. /* Return through std_return after this point. */
  1999. xfs_itrace_entry(dp);
  2000. mp = dp->i_mount;
  2001. udqp = gdqp = NULL;
  2002. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2003. prid = dp->i_d.di_projid;
  2004. else
  2005. prid = (xfs_prid_t)dfltprid;
  2006. /*
  2007. * Make sure that we have allocated dquot(s) on disk.
  2008. */
  2009. error = XFS_QM_DQVOPALLOC(mp, dp,
  2010. current_fsuid(credp), current_fsgid(credp), prid,
  2011. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2012. if (error)
  2013. goto std_return;
  2014. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  2015. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2016. resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
  2017. error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
  2018. XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
  2019. if (error == ENOSPC) {
  2020. resblks = 0;
  2021. error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
  2022. XFS_TRANS_PERM_LOG_RES,
  2023. XFS_MKDIR_LOG_COUNT);
  2024. }
  2025. if (error) {
  2026. cancel_flags = 0;
  2027. goto error_return;
  2028. }
  2029. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  2030. unlock_dp_on_error = B_TRUE;
  2031. /*
  2032. * Check for directory link count overflow.
  2033. */
  2034. if (dp->i_d.di_nlink >= XFS_MAXLINK) {
  2035. error = XFS_ERROR(EMLINK);
  2036. goto error_return;
  2037. }
  2038. /*
  2039. * Reserve disk quota and the inode.
  2040. */
  2041. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2042. if (error)
  2043. goto error_return;
  2044. error = xfs_dir_canenter(tp, dp, dir_name, resblks);
  2045. if (error)
  2046. goto error_return;
  2047. /*
  2048. * create the directory inode.
  2049. */
  2050. error = xfs_dir_ialloc(&tp, dp, mode, 2,
  2051. 0, credp, prid, resblks > 0,
  2052. &cdp, NULL);
  2053. if (error) {
  2054. if (error == ENOSPC)
  2055. goto error_return;
  2056. goto abort_return;
  2057. }
  2058. xfs_itrace_ref(cdp);
  2059. /*
  2060. * Now we add the directory inode to the transaction.
  2061. * We waited until now since xfs_dir_ialloc might start
  2062. * a new transaction. Had we joined the transaction
  2063. * earlier, the locks might have gotten released. An error
  2064. * from here on will result in the transaction cancel
  2065. * unlocking dp so don't do it explicitly in the error path.
  2066. */
  2067. IHOLD(dp);
  2068. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2069. unlock_dp_on_error = B_FALSE;
  2070. XFS_BMAP_INIT(&free_list, &first_block);
  2071. error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
  2072. &first_block, &free_list, resblks ?
  2073. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  2074. if (error) {
  2075. ASSERT(error != ENOSPC);
  2076. goto error1;
  2077. }
  2078. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2079. /*
  2080. * Bump the in memory version number of the parent directory
  2081. * so that other processes accessing it will recognize that
  2082. * the directory has changed.
  2083. */
  2084. dp->i_gen++;
  2085. error = xfs_dir_init(tp, cdp, dp);
  2086. if (error)
  2087. goto error2;
  2088. cdp->i_gen = 1;
  2089. error = xfs_bumplink(tp, dp);
  2090. if (error)
  2091. goto error2;
  2092. created = B_TRUE;
  2093. *ipp = cdp;
  2094. IHOLD(cdp);
  2095. /*
  2096. * Attach the dquots to the new inode and modify the icount incore.
  2097. */
  2098. XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
  2099. /*
  2100. * If this is a synchronous mount, make sure that the
  2101. * mkdir transaction goes to disk before returning to
  2102. * the user.
  2103. */
  2104. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2105. xfs_trans_set_sync(tp);
  2106. }
  2107. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2108. if (error) {
  2109. IRELE(cdp);
  2110. goto error2;
  2111. }
  2112. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2113. XFS_QM_DQRELE(mp, udqp);
  2114. XFS_QM_DQRELE(mp, gdqp);
  2115. if (error) {
  2116. IRELE(cdp);
  2117. }
  2118. /* Fall through to std_return with error = 0 or errno from
  2119. * xfs_trans_commit. */
  2120. std_return:
  2121. if ((created || (error != 0 && dm_event_sent != 0)) &&
  2122. DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
  2123. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  2124. dp, DM_RIGHT_NULL,
  2125. created ? cdp : NULL,
  2126. DM_RIGHT_NULL,
  2127. dir_name->name, NULL,
  2128. mode, error, 0);
  2129. }
  2130. return error;
  2131. error2:
  2132. error1:
  2133. xfs_bmap_cancel(&free_list);
  2134. abort_return:
  2135. cancel_flags |= XFS_TRANS_ABORT;
  2136. error_return:
  2137. xfs_trans_cancel(tp, cancel_flags);
  2138. XFS_QM_DQRELE(mp, udqp);
  2139. XFS_QM_DQRELE(mp, gdqp);
  2140. if (unlock_dp_on_error)
  2141. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2142. goto std_return;
  2143. }
  2144. int
  2145. xfs_symlink(
  2146. xfs_inode_t *dp,
  2147. struct xfs_name *link_name,
  2148. const char *target_path,
  2149. mode_t mode,
  2150. xfs_inode_t **ipp,
  2151. cred_t *credp)
  2152. {
  2153. xfs_mount_t *mp = dp->i_mount;
  2154. xfs_trans_t *tp;
  2155. xfs_inode_t *ip;
  2156. int error;
  2157. int pathlen;
  2158. xfs_bmap_free_t free_list;
  2159. xfs_fsblock_t first_block;
  2160. boolean_t unlock_dp_on_error = B_FALSE;
  2161. uint cancel_flags;
  2162. int committed;
  2163. xfs_fileoff_t first_fsb;
  2164. xfs_filblks_t fs_blocks;
  2165. int nmaps;
  2166. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  2167. xfs_daddr_t d;
  2168. const char *cur_chunk;
  2169. int byte_cnt;
  2170. int n;
  2171. xfs_buf_t *bp;
  2172. xfs_prid_t prid;
  2173. struct xfs_dquot *udqp, *gdqp;
  2174. uint resblks;
  2175. *ipp = NULL;
  2176. error = 0;
  2177. ip = NULL;
  2178. tp = NULL;
  2179. xfs_itrace_entry(dp);
  2180. if (XFS_FORCED_SHUTDOWN(mp))
  2181. return XFS_ERROR(EIO);
  2182. /*
  2183. * Check component lengths of the target path name.
  2184. */
  2185. pathlen = strlen(target_path);
  2186. if (pathlen >= MAXPATHLEN) /* total string too long */
  2187. return XFS_ERROR(ENAMETOOLONG);
  2188. if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
  2189. error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
  2190. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  2191. link_name->name, target_path, 0, 0, 0);
  2192. if (error)
  2193. return error;
  2194. }
  2195. /* Return through std_return after this point. */
  2196. udqp = gdqp = NULL;
  2197. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2198. prid = dp->i_d.di_projid;
  2199. else
  2200. prid = (xfs_prid_t)dfltprid;
  2201. /*
  2202. * Make sure that we have allocated dquot(s) on disk.
  2203. */
  2204. error = XFS_QM_DQVOPALLOC(mp, dp,
  2205. current_fsuid(credp), current_fsgid(credp), prid,
  2206. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2207. if (error)
  2208. goto std_return;
  2209. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  2210. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2211. /*
  2212. * The symlink will fit into the inode data fork?
  2213. * There can't be any attributes so we get the whole variable part.
  2214. */
  2215. if (pathlen <= XFS_LITINO(mp))
  2216. fs_blocks = 0;
  2217. else
  2218. fs_blocks = XFS_B_TO_FSB(mp, pathlen);
  2219. resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
  2220. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  2221. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2222. if (error == ENOSPC && fs_blocks == 0) {
  2223. resblks = 0;
  2224. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  2225. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2226. }
  2227. if (error) {
  2228. cancel_flags = 0;
  2229. goto error_return;
  2230. }
  2231. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  2232. unlock_dp_on_error = B_TRUE;
  2233. /*
  2234. * Check whether the directory allows new symlinks or not.
  2235. */
  2236. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  2237. error = XFS_ERROR(EPERM);
  2238. goto error_return;
  2239. }
  2240. /*
  2241. * Reserve disk quota : blocks and inode.
  2242. */
  2243. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2244. if (error)
  2245. goto error_return;
  2246. /*
  2247. * Check for ability to enter directory entry, if no space reserved.
  2248. */
  2249. error = xfs_dir_canenter(tp, dp, link_name, resblks);
  2250. if (error)
  2251. goto error_return;
  2252. /*
  2253. * Initialize the bmap freelist prior to calling either
  2254. * bmapi or the directory create code.
  2255. */
  2256. XFS_BMAP_INIT(&free_list, &first_block);
  2257. /*
  2258. * Allocate an inode for the symlink.
  2259. */
  2260. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
  2261. 1, 0, credp, prid, resblks > 0, &ip, NULL);
  2262. if (error) {
  2263. if (error == ENOSPC)
  2264. goto error_return;
  2265. goto error1;
  2266. }
  2267. xfs_itrace_ref(ip);
  2268. /*
  2269. * An error after we've joined dp to the transaction will result in the
  2270. * transaction cancel unlocking dp so don't do it explicitly in the
  2271. * error path.
  2272. */
  2273. IHOLD(dp);
  2274. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2275. unlock_dp_on_error = B_FALSE;
  2276. /*
  2277. * Also attach the dquot(s) to it, if applicable.
  2278. */
  2279. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  2280. if (resblks)
  2281. resblks -= XFS_IALLOC_SPACE_RES(mp);
  2282. /*
  2283. * If the symlink will fit into the inode, write it inline.
  2284. */
  2285. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  2286. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  2287. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  2288. ip->i_d.di_size = pathlen;
  2289. /*
  2290. * The inode was initially created in extent format.
  2291. */
  2292. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  2293. ip->i_df.if_flags |= XFS_IFINLINE;
  2294. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  2295. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  2296. } else {
  2297. first_fsb = 0;
  2298. nmaps = SYMLINK_MAPS;
  2299. error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
  2300. XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
  2301. &first_block, resblks, mval, &nmaps,
  2302. &free_list, NULL);
  2303. if (error) {
  2304. goto error1;
  2305. }
  2306. if (resblks)
  2307. resblks -= fs_blocks;
  2308. ip->i_d.di_size = pathlen;
  2309. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2310. cur_chunk = target_path;
  2311. for (n = 0; n < nmaps; n++) {
  2312. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  2313. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  2314. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  2315. BTOBB(byte_cnt), 0);
  2316. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  2317. if (pathlen < byte_cnt) {
  2318. byte_cnt = pathlen;
  2319. }
  2320. pathlen -= byte_cnt;
  2321. memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
  2322. cur_chunk += byte_cnt;
  2323. xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
  2324. }
  2325. }
  2326. /*
  2327. * Create the directory entry for the symlink.
  2328. */
  2329. error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
  2330. &first_block, &free_list, resblks);
  2331. if (error)
  2332. goto error1;
  2333. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2334. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2335. /*
  2336. * Bump the in memory version number of the parent directory
  2337. * so that other processes accessing it will recognize that
  2338. * the directory has changed.
  2339. */
  2340. dp->i_gen++;
  2341. /*
  2342. * If this is a synchronous mount, make sure that the
  2343. * symlink transaction goes to disk before returning to
  2344. * the user.
  2345. */
  2346. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2347. xfs_trans_set_sync(tp);
  2348. }
  2349. /*
  2350. * xfs_trans_commit normally decrements the vnode ref count
  2351. * when it unlocks the inode. Since we want to return the
  2352. * vnode to the caller, we bump the vnode ref count now.
  2353. */
  2354. IHOLD(ip);
  2355. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2356. if (error) {
  2357. goto error2;
  2358. }
  2359. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2360. XFS_QM_DQRELE(mp, udqp);
  2361. XFS_QM_DQRELE(mp, gdqp);
  2362. /* Fall through to std_return with error = 0 or errno from
  2363. * xfs_trans_commit */
  2364. std_return:
  2365. if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
  2366. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
  2367. dp, DM_RIGHT_NULL,
  2368. error ? NULL : ip,
  2369. DM_RIGHT_NULL, link_name->name,
  2370. target_path, 0, error, 0);
  2371. }
  2372. if (!error)
  2373. *ipp = ip;
  2374. return error;
  2375. error2:
  2376. IRELE(ip);
  2377. error1:
  2378. xfs_bmap_cancel(&free_list);
  2379. cancel_flags |= XFS_TRANS_ABORT;
  2380. error_return:
  2381. xfs_trans_cancel(tp, cancel_flags);
  2382. XFS_QM_DQRELE(mp, udqp);
  2383. XFS_QM_DQRELE(mp, gdqp);
  2384. if (unlock_dp_on_error)
  2385. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2386. goto std_return;
  2387. }
  2388. int
  2389. xfs_inode_flush(
  2390. xfs_inode_t *ip,
  2391. int flags)
  2392. {
  2393. xfs_mount_t *mp = ip->i_mount;
  2394. int error = 0;
  2395. if (XFS_FORCED_SHUTDOWN(mp))
  2396. return XFS_ERROR(EIO);
  2397. /*
  2398. * Bypass inodes which have already been cleaned by
  2399. * the inode flush clustering code inside xfs_iflush
  2400. */
  2401. if (xfs_inode_clean(ip))
  2402. return 0;
  2403. /*
  2404. * We make this non-blocking if the inode is contended,
  2405. * return EAGAIN to indicate to the caller that they
  2406. * did not succeed. This prevents the flush path from
  2407. * blocking on inodes inside another operation right
  2408. * now, they get caught later by xfs_sync.
  2409. */
  2410. if (flags & FLUSH_SYNC) {
  2411. xfs_ilock(ip, XFS_ILOCK_SHARED);
  2412. xfs_iflock(ip);
  2413. } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  2414. if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
  2415. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  2416. return EAGAIN;
  2417. }
  2418. } else {
  2419. return EAGAIN;
  2420. }
  2421. error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
  2422. : XFS_IFLUSH_ASYNC_NOBLOCK);
  2423. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  2424. return error;
  2425. }
  2426. int
  2427. xfs_set_dmattrs(
  2428. xfs_inode_t *ip,
  2429. u_int evmask,
  2430. u_int16_t state)
  2431. {
  2432. xfs_mount_t *mp = ip->i_mount;
  2433. xfs_trans_t *tp;
  2434. int error;
  2435. if (!capable(CAP_SYS_ADMIN))
  2436. return XFS_ERROR(EPERM);
  2437. if (XFS_FORCED_SHUTDOWN(mp))
  2438. return XFS_ERROR(EIO);
  2439. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  2440. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  2441. if (error) {
  2442. xfs_trans_cancel(tp, 0);
  2443. return error;
  2444. }
  2445. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2446. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2447. ip->i_d.di_dmevmask = evmask;
  2448. ip->i_d.di_dmstate = state;
  2449. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2450. IHOLD(ip);
  2451. error = xfs_trans_commit(tp, 0);
  2452. return error;
  2453. }
  2454. int
  2455. xfs_reclaim(
  2456. xfs_inode_t *ip)
  2457. {
  2458. xfs_itrace_entry(ip);
  2459. ASSERT(!VN_MAPPED(VFS_I(ip)));
  2460. /* bad inode, get out here ASAP */
  2461. if (VN_BAD(VFS_I(ip))) {
  2462. xfs_ireclaim(ip);
  2463. return 0;
  2464. }
  2465. vn_iowait(ip);
  2466. ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
  2467. /*
  2468. * Make sure the atime in the XFS inode is correct before freeing the
  2469. * Linux inode.
  2470. */
  2471. xfs_synchronize_atime(ip);
  2472. /*
  2473. * If we have nothing to flush with this inode then complete the
  2474. * teardown now, otherwise break the link between the xfs inode and the
  2475. * linux inode and clean up the xfs inode later. This avoids flushing
  2476. * the inode to disk during the delete operation itself.
  2477. *
  2478. * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
  2479. * first to ensure that xfs_iunpin() will never see an xfs inode
  2480. * that has a linux inode being reclaimed. Synchronisation is provided
  2481. * by the i_flags_lock.
  2482. */
  2483. if (!ip->i_update_core && (ip->i_itemp == NULL)) {
  2484. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2485. xfs_iflock(ip);
  2486. return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
  2487. } else {
  2488. xfs_mount_t *mp = ip->i_mount;
  2489. /* Protect sync and unpin from us */
  2490. XFS_MOUNT_ILOCK(mp);
  2491. spin_lock(&ip->i_flags_lock);
  2492. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  2493. VFS_I(ip)->i_private = NULL;
  2494. ip->i_vnode = NULL;
  2495. spin_unlock(&ip->i_flags_lock);
  2496. list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
  2497. XFS_MOUNT_IUNLOCK(mp);
  2498. }
  2499. return 0;
  2500. }
  2501. int
  2502. xfs_finish_reclaim(
  2503. xfs_inode_t *ip,
  2504. int locked,
  2505. int sync_mode)
  2506. {
  2507. xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
  2508. struct inode *vp = VFS_I(ip);
  2509. if (vp && VN_BAD(vp))
  2510. goto reclaim;
  2511. /* The hash lock here protects a thread in xfs_iget_core from
  2512. * racing with us on linking the inode back with a vnode.
  2513. * Once we have the XFS_IRECLAIM flag set it will not touch
  2514. * us.
  2515. */
  2516. write_lock(&pag->pag_ici_lock);
  2517. spin_lock(&ip->i_flags_lock);
  2518. if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
  2519. (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
  2520. spin_unlock(&ip->i_flags_lock);
  2521. write_unlock(&pag->pag_ici_lock);
  2522. if (locked) {
  2523. xfs_ifunlock(ip);
  2524. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2525. }
  2526. return 1;
  2527. }
  2528. __xfs_iflags_set(ip, XFS_IRECLAIM);
  2529. spin_unlock(&ip->i_flags_lock);
  2530. write_unlock(&pag->pag_ici_lock);
  2531. xfs_put_perag(ip->i_mount, pag);
  2532. /*
  2533. * If the inode is still dirty, then flush it out. If the inode
  2534. * is not in the AIL, then it will be OK to flush it delwri as
  2535. * long as xfs_iflush() does not keep any references to the inode.
  2536. * We leave that decision up to xfs_iflush() since it has the
  2537. * knowledge of whether it's OK to simply do a delwri flush of
  2538. * the inode or whether we need to wait until the inode is
  2539. * pulled from the AIL.
  2540. * We get the flush lock regardless, though, just to make sure
  2541. * we don't free it while it is being flushed.
  2542. */
  2543. if (!locked) {
  2544. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2545. xfs_iflock(ip);
  2546. }
  2547. /*
  2548. * In the case of a forced shutdown we rely on xfs_iflush() to
  2549. * wait for the inode to be unpinned before returning an error.
  2550. */
  2551. if (xfs_iflush(ip, sync_mode) == 0) {
  2552. /* synchronize with xfs_iflush_done */
  2553. xfs_iflock(ip);
  2554. xfs_ifunlock(ip);
  2555. }
  2556. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2557. reclaim:
  2558. xfs_ireclaim(ip);
  2559. return 0;
  2560. }
  2561. int
  2562. xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
  2563. {
  2564. int purged;
  2565. xfs_inode_t *ip, *n;
  2566. int done = 0;
  2567. while (!done) {
  2568. purged = 0;
  2569. XFS_MOUNT_ILOCK(mp);
  2570. list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
  2571. if (noblock) {
  2572. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
  2573. continue;
  2574. if (xfs_ipincount(ip) ||
  2575. !xfs_iflock_nowait(ip)) {
  2576. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2577. continue;
  2578. }
  2579. }
  2580. XFS_MOUNT_IUNLOCK(mp);
  2581. if (xfs_finish_reclaim(ip, noblock,
  2582. XFS_IFLUSH_DELWRI_ELSE_ASYNC))
  2583. delay(1);
  2584. purged = 1;
  2585. break;
  2586. }
  2587. done = !purged;
  2588. }
  2589. XFS_MOUNT_IUNLOCK(mp);
  2590. return 0;
  2591. }
  2592. /*
  2593. * xfs_alloc_file_space()
  2594. * This routine allocates disk space for the given file.
  2595. *
  2596. * If alloc_type == 0, this request is for an ALLOCSP type
  2597. * request which will change the file size. In this case, no
  2598. * DMAPI event will be generated by the call. A TRUNCATE event
  2599. * will be generated later by xfs_setattr.
  2600. *
  2601. * If alloc_type != 0, this request is for a RESVSP type
  2602. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  2603. * lower block boundary byte address is less than the file's
  2604. * length.
  2605. *
  2606. * RETURNS:
  2607. * 0 on success
  2608. * errno on error
  2609. *
  2610. */
  2611. STATIC int
  2612. xfs_alloc_file_space(
  2613. xfs_inode_t *ip,
  2614. xfs_off_t offset,
  2615. xfs_off_t len,
  2616. int alloc_type,
  2617. int attr_flags)
  2618. {
  2619. xfs_mount_t *mp = ip->i_mount;
  2620. xfs_off_t count;
  2621. xfs_filblks_t allocated_fsb;
  2622. xfs_filblks_t allocatesize_fsb;
  2623. xfs_extlen_t extsz, temp;
  2624. xfs_fileoff_t startoffset_fsb;
  2625. xfs_fsblock_t firstfsb;
  2626. int nimaps;
  2627. int bmapi_flag;
  2628. int quota_flag;
  2629. int rt;
  2630. xfs_trans_t *tp;
  2631. xfs_bmbt_irec_t imaps[1], *imapp;
  2632. xfs_bmap_free_t free_list;
  2633. uint qblocks, resblks, resrtextents;
  2634. int committed;
  2635. int error;
  2636. xfs_itrace_entry(ip);
  2637. if (XFS_FORCED_SHUTDOWN(mp))
  2638. return XFS_ERROR(EIO);
  2639. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  2640. return error;
  2641. if (len <= 0)
  2642. return XFS_ERROR(EINVAL);
  2643. rt = XFS_IS_REALTIME_INODE(ip);
  2644. extsz = xfs_get_extsz_hint(ip);
  2645. count = len;
  2646. imapp = &imaps[0];
  2647. nimaps = 1;
  2648. bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
  2649. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  2650. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  2651. /* Generate a DMAPI event if needed. */
  2652. if (alloc_type != 0 && offset < ip->i_size &&
  2653. (attr_flags & XFS_ATTR_DMI) == 0 &&
  2654. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  2655. xfs_off_t end_dmi_offset;
  2656. end_dmi_offset = offset+len;
  2657. if (end_dmi_offset > ip->i_size)
  2658. end_dmi_offset = ip->i_size;
  2659. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
  2660. end_dmi_offset - offset, 0, NULL);
  2661. if (error)
  2662. return error;
  2663. }
  2664. /*
  2665. * Allocate file space until done or until there is an error
  2666. */
  2667. retry:
  2668. while (allocatesize_fsb && !error) {
  2669. xfs_fileoff_t s, e;
  2670. /*
  2671. * Determine space reservations for data/realtime.
  2672. */
  2673. if (unlikely(extsz)) {
  2674. s = startoffset_fsb;
  2675. do_div(s, extsz);
  2676. s *= extsz;
  2677. e = startoffset_fsb + allocatesize_fsb;
  2678. if ((temp = do_mod(startoffset_fsb, extsz)))
  2679. e += temp;
  2680. if ((temp = do_mod(e, extsz)))
  2681. e += extsz - temp;
  2682. } else {
  2683. s = 0;
  2684. e = allocatesize_fsb;
  2685. }
  2686. if (unlikely(rt)) {
  2687. resrtextents = qblocks = (uint)(e - s);
  2688. resrtextents /= mp->m_sb.sb_rextsize;
  2689. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  2690. quota_flag = XFS_QMOPT_RES_RTBLKS;
  2691. } else {
  2692. resrtextents = 0;
  2693. resblks = qblocks = \
  2694. XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
  2695. quota_flag = XFS_QMOPT_RES_REGBLKS;
  2696. }
  2697. /*
  2698. * Allocate and setup the transaction.
  2699. */
  2700. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  2701. error = xfs_trans_reserve(tp, resblks,
  2702. XFS_WRITE_LOG_RES(mp), resrtextents,
  2703. XFS_TRANS_PERM_LOG_RES,
  2704. XFS_WRITE_LOG_COUNT);
  2705. /*
  2706. * Check for running out of space
  2707. */
  2708. if (error) {
  2709. /*
  2710. * Free the transaction structure.
  2711. */
  2712. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  2713. xfs_trans_cancel(tp, 0);
  2714. break;
  2715. }
  2716. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2717. error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
  2718. qblocks, 0, quota_flag);
  2719. if (error)
  2720. goto error1;
  2721. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2722. xfs_trans_ihold(tp, ip);
  2723. /*
  2724. * Issue the xfs_bmapi() call to allocate the blocks
  2725. */
  2726. XFS_BMAP_INIT(&free_list, &firstfsb);
  2727. error = xfs_bmapi(tp, ip, startoffset_fsb,
  2728. allocatesize_fsb, bmapi_flag,
  2729. &firstfsb, 0, imapp, &nimaps,
  2730. &free_list, NULL);
  2731. if (error) {
  2732. goto error0;
  2733. }
  2734. /*
  2735. * Complete the transaction
  2736. */
  2737. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2738. if (error) {
  2739. goto error0;
  2740. }
  2741. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2742. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2743. if (error) {
  2744. break;
  2745. }
  2746. allocated_fsb = imapp->br_blockcount;
  2747. if (nimaps == 0) {
  2748. error = XFS_ERROR(ENOSPC);
  2749. break;
  2750. }
  2751. startoffset_fsb += allocated_fsb;
  2752. allocatesize_fsb -= allocated_fsb;
  2753. }
  2754. dmapi_enospc_check:
  2755. if (error == ENOSPC && (attr_flags & XFS_ATTR_DMI) == 0 &&
  2756. DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
  2757. error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
  2758. ip, DM_RIGHT_NULL,
  2759. ip, DM_RIGHT_NULL,
  2760. NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
  2761. if (error == 0)
  2762. goto retry; /* Maybe DMAPI app. has made space */
  2763. /* else fall through with error from XFS_SEND_DATA */
  2764. }
  2765. return error;
  2766. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  2767. xfs_bmap_cancel(&free_list);
  2768. XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
  2769. error1: /* Just cancel transaction */
  2770. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  2771. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2772. goto dmapi_enospc_check;
  2773. }
  2774. /*
  2775. * Zero file bytes between startoff and endoff inclusive.
  2776. * The iolock is held exclusive and no blocks are buffered.
  2777. */
  2778. STATIC int
  2779. xfs_zero_remaining_bytes(
  2780. xfs_inode_t *ip,
  2781. xfs_off_t startoff,
  2782. xfs_off_t endoff)
  2783. {
  2784. xfs_bmbt_irec_t imap;
  2785. xfs_fileoff_t offset_fsb;
  2786. xfs_off_t lastoffset;
  2787. xfs_off_t offset;
  2788. xfs_buf_t *bp;
  2789. xfs_mount_t *mp = ip->i_mount;
  2790. int nimap;
  2791. int error = 0;
  2792. bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
  2793. XFS_IS_REALTIME_INODE(ip) ?
  2794. mp->m_rtdev_targp : mp->m_ddev_targp);
  2795. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  2796. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  2797. nimap = 1;
  2798. error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
  2799. NULL, 0, &imap, &nimap, NULL, NULL);
  2800. if (error || nimap < 1)
  2801. break;
  2802. ASSERT(imap.br_blockcount >= 1);
  2803. ASSERT(imap.br_startoff == offset_fsb);
  2804. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  2805. if (lastoffset > endoff)
  2806. lastoffset = endoff;
  2807. if (imap.br_startblock == HOLESTARTBLOCK)
  2808. continue;
  2809. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  2810. if (imap.br_state == XFS_EXT_UNWRITTEN)
  2811. continue;
  2812. XFS_BUF_UNDONE(bp);
  2813. XFS_BUF_UNWRITE(bp);
  2814. XFS_BUF_READ(bp);
  2815. XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
  2816. xfsbdstrat(mp, bp);
  2817. error = xfs_iowait(bp);
  2818. if (error) {
  2819. xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
  2820. mp, bp, XFS_BUF_ADDR(bp));
  2821. break;
  2822. }
  2823. memset(XFS_BUF_PTR(bp) +
  2824. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  2825. 0, lastoffset - offset + 1);
  2826. XFS_BUF_UNDONE(bp);
  2827. XFS_BUF_UNREAD(bp);
  2828. XFS_BUF_WRITE(bp);
  2829. xfsbdstrat(mp, bp);
  2830. error = xfs_iowait(bp);
  2831. if (error) {
  2832. xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
  2833. mp, bp, XFS_BUF_ADDR(bp));
  2834. break;
  2835. }
  2836. }
  2837. xfs_buf_free(bp);
  2838. return error;
  2839. }
  2840. /*
  2841. * xfs_free_file_space()
  2842. * This routine frees disk space for the given file.
  2843. *
  2844. * This routine is only called by xfs_change_file_space
  2845. * for an UNRESVSP type call.
  2846. *
  2847. * RETURNS:
  2848. * 0 on success
  2849. * errno on error
  2850. *
  2851. */
  2852. STATIC int
  2853. xfs_free_file_space(
  2854. xfs_inode_t *ip,
  2855. xfs_off_t offset,
  2856. xfs_off_t len,
  2857. int attr_flags)
  2858. {
  2859. int committed;
  2860. int done;
  2861. xfs_off_t end_dmi_offset;
  2862. xfs_fileoff_t endoffset_fsb;
  2863. int error;
  2864. xfs_fsblock_t firstfsb;
  2865. xfs_bmap_free_t free_list;
  2866. xfs_bmbt_irec_t imap;
  2867. xfs_off_t ioffset;
  2868. xfs_extlen_t mod=0;
  2869. xfs_mount_t *mp;
  2870. int nimap;
  2871. uint resblks;
  2872. uint rounding;
  2873. int rt;
  2874. xfs_fileoff_t startoffset_fsb;
  2875. xfs_trans_t *tp;
  2876. int need_iolock = 1;
  2877. mp = ip->i_mount;
  2878. xfs_itrace_entry(ip);
  2879. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  2880. return error;
  2881. error = 0;
  2882. if (len <= 0) /* if nothing being freed */
  2883. return error;
  2884. rt = XFS_IS_REALTIME_INODE(ip);
  2885. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  2886. end_dmi_offset = offset + len;
  2887. endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
  2888. if (offset < ip->i_size && (attr_flags & XFS_ATTR_DMI) == 0 &&
  2889. DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
  2890. if (end_dmi_offset > ip->i_size)
  2891. end_dmi_offset = ip->i_size;
  2892. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
  2893. offset, end_dmi_offset - offset,
  2894. AT_DELAY_FLAG(attr_flags), NULL);
  2895. if (error)
  2896. return error;
  2897. }
  2898. if (attr_flags & XFS_ATTR_NOLOCK)
  2899. need_iolock = 0;
  2900. if (need_iolock) {
  2901. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  2902. vn_iowait(ip); /* wait for the completion of any pending DIOs */
  2903. }
  2904. rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  2905. ioffset = offset & ~(rounding - 1);
  2906. if (VN_CACHED(VFS_I(ip)) != 0) {
  2907. xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
  2908. error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
  2909. if (error)
  2910. goto out_unlock_iolock;
  2911. }
  2912. /*
  2913. * Need to zero the stuff we're not freeing, on disk.
  2914. * If its a realtime file & can't use unwritten extents then we
  2915. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  2916. * will take care of it for us.
  2917. */
  2918. if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  2919. nimap = 1;
  2920. error = xfs_bmapi(NULL, ip, startoffset_fsb,
  2921. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  2922. if (error)
  2923. goto out_unlock_iolock;
  2924. ASSERT(nimap == 0 || nimap == 1);
  2925. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  2926. xfs_daddr_t block;
  2927. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  2928. block = imap.br_startblock;
  2929. mod = do_div(block, mp->m_sb.sb_rextsize);
  2930. if (mod)
  2931. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  2932. }
  2933. nimap = 1;
  2934. error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
  2935. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  2936. if (error)
  2937. goto out_unlock_iolock;
  2938. ASSERT(nimap == 0 || nimap == 1);
  2939. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  2940. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  2941. mod++;
  2942. if (mod && (mod != mp->m_sb.sb_rextsize))
  2943. endoffset_fsb -= mod;
  2944. }
  2945. }
  2946. if ((done = (endoffset_fsb <= startoffset_fsb)))
  2947. /*
  2948. * One contiguous piece to clear
  2949. */
  2950. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  2951. else {
  2952. /*
  2953. * Some full blocks, possibly two pieces to clear
  2954. */
  2955. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  2956. error = xfs_zero_remaining_bytes(ip, offset,
  2957. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  2958. if (!error &&
  2959. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  2960. error = xfs_zero_remaining_bytes(ip,
  2961. XFS_FSB_TO_B(mp, endoffset_fsb),
  2962. offset + len - 1);
  2963. }
  2964. /*
  2965. * free file space until done or until there is an error
  2966. */
  2967. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  2968. while (!error && !done) {
  2969. /*
  2970. * allocate and setup the transaction. Allow this
  2971. * transaction to dip into the reserve blocks to ensure
  2972. * the freeing of the space succeeds at ENOSPC.
  2973. */
  2974. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  2975. tp->t_flags |= XFS_TRANS_RESERVE;
  2976. error = xfs_trans_reserve(tp,
  2977. resblks,
  2978. XFS_WRITE_LOG_RES(mp),
  2979. 0,
  2980. XFS_TRANS_PERM_LOG_RES,
  2981. XFS_WRITE_LOG_COUNT);
  2982. /*
  2983. * check for running out of space
  2984. */
  2985. if (error) {
  2986. /*
  2987. * Free the transaction structure.
  2988. */
  2989. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  2990. xfs_trans_cancel(tp, 0);
  2991. break;
  2992. }
  2993. xfs_ilock(ip, XFS_ILOCK_EXCL);
  2994. error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
  2995. ip->i_udquot, ip->i_gdquot, resblks, 0,
  2996. XFS_QMOPT_RES_REGBLKS);
  2997. if (error)
  2998. goto error1;
  2999. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3000. xfs_trans_ihold(tp, ip);
  3001. /*
  3002. * issue the bunmapi() call to free the blocks
  3003. */
  3004. XFS_BMAP_INIT(&free_list, &firstfsb);
  3005. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  3006. endoffset_fsb - startoffset_fsb,
  3007. 0, 2, &firstfsb, &free_list, NULL, &done);
  3008. if (error) {
  3009. goto error0;
  3010. }
  3011. /*
  3012. * complete the transaction
  3013. */
  3014. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3015. if (error) {
  3016. goto error0;
  3017. }
  3018. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3019. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3020. }
  3021. out_unlock_iolock:
  3022. if (need_iolock)
  3023. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  3024. return error;
  3025. error0:
  3026. xfs_bmap_cancel(&free_list);
  3027. error1:
  3028. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3029. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  3030. XFS_ILOCK_EXCL);
  3031. return error;
  3032. }
  3033. /*
  3034. * xfs_change_file_space()
  3035. * This routine allocates or frees disk space for the given file.
  3036. * The user specified parameters are checked for alignment and size
  3037. * limitations.
  3038. *
  3039. * RETURNS:
  3040. * 0 on success
  3041. * errno on error
  3042. *
  3043. */
  3044. int
  3045. xfs_change_file_space(
  3046. xfs_inode_t *ip,
  3047. int cmd,
  3048. xfs_flock64_t *bf,
  3049. xfs_off_t offset,
  3050. cred_t *credp,
  3051. int attr_flags)
  3052. {
  3053. xfs_mount_t *mp = ip->i_mount;
  3054. int clrprealloc;
  3055. int error;
  3056. xfs_fsize_t fsize;
  3057. int setprealloc;
  3058. xfs_off_t startoffset;
  3059. xfs_off_t llen;
  3060. xfs_trans_t *tp;
  3061. struct iattr iattr;
  3062. xfs_itrace_entry(ip);
  3063. if (!S_ISREG(ip->i_d.di_mode))
  3064. return XFS_ERROR(EINVAL);
  3065. switch (bf->l_whence) {
  3066. case 0: /*SEEK_SET*/
  3067. break;
  3068. case 1: /*SEEK_CUR*/
  3069. bf->l_start += offset;
  3070. break;
  3071. case 2: /*SEEK_END*/
  3072. bf->l_start += ip->i_size;
  3073. break;
  3074. default:
  3075. return XFS_ERROR(EINVAL);
  3076. }
  3077. llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
  3078. if ( (bf->l_start < 0)
  3079. || (bf->l_start > XFS_MAXIOFFSET(mp))
  3080. || (bf->l_start + llen < 0)
  3081. || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
  3082. return XFS_ERROR(EINVAL);
  3083. bf->l_whence = 0;
  3084. startoffset = bf->l_start;
  3085. fsize = ip->i_size;
  3086. /*
  3087. * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
  3088. * file space.
  3089. * These calls do NOT zero the data space allocated to the file,
  3090. * nor do they change the file size.
  3091. *
  3092. * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
  3093. * space.
  3094. * These calls cause the new file data to be zeroed and the file
  3095. * size to be changed.
  3096. */
  3097. setprealloc = clrprealloc = 0;
  3098. switch (cmd) {
  3099. case XFS_IOC_RESVSP:
  3100. case XFS_IOC_RESVSP64:
  3101. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  3102. 1, attr_flags);
  3103. if (error)
  3104. return error;
  3105. setprealloc = 1;
  3106. break;
  3107. case XFS_IOC_UNRESVSP:
  3108. case XFS_IOC_UNRESVSP64:
  3109. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  3110. attr_flags)))
  3111. return error;
  3112. break;
  3113. case XFS_IOC_ALLOCSP:
  3114. case XFS_IOC_ALLOCSP64:
  3115. case XFS_IOC_FREESP:
  3116. case XFS_IOC_FREESP64:
  3117. if (startoffset > fsize) {
  3118. error = xfs_alloc_file_space(ip, fsize,
  3119. startoffset - fsize, 0, attr_flags);
  3120. if (error)
  3121. break;
  3122. }
  3123. iattr.ia_valid = ATTR_SIZE;
  3124. iattr.ia_size = startoffset;
  3125. error = xfs_setattr(ip, &iattr, attr_flags, credp);
  3126. if (error)
  3127. return error;
  3128. clrprealloc = 1;
  3129. break;
  3130. default:
  3131. ASSERT(0);
  3132. return XFS_ERROR(EINVAL);
  3133. }
  3134. /*
  3135. * update the inode timestamp, mode, and prealloc flag bits
  3136. */
  3137. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  3138. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  3139. 0, 0, 0))) {
  3140. /* ASSERT(0); */
  3141. xfs_trans_cancel(tp, 0);
  3142. return error;
  3143. }
  3144. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3145. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3146. xfs_trans_ihold(tp, ip);
  3147. if ((attr_flags & XFS_ATTR_DMI) == 0) {
  3148. ip->i_d.di_mode &= ~S_ISUID;
  3149. /*
  3150. * Note that we don't have to worry about mandatory
  3151. * file locking being disabled here because we only
  3152. * clear the S_ISGID bit if the Group execute bit is
  3153. * on, but if it was on then mandatory locking wouldn't
  3154. * have been enabled.
  3155. */
  3156. if (ip->i_d.di_mode & S_IXGRP)
  3157. ip->i_d.di_mode &= ~S_ISGID;
  3158. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  3159. }
  3160. if (setprealloc)
  3161. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  3162. else if (clrprealloc)
  3163. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  3164. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3165. xfs_trans_set_sync(tp);
  3166. error = xfs_trans_commit(tp, 0);
  3167. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3168. return error;
  3169. }