xfs_vnodeops.c 88 KB

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