xfs_vnodeops.c 79 KB

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