inode.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942
  1. /*
  2. * fs/cifs/inode.c
  3. *
  4. * Copyright (C) International Business Machines Corp., 2002,2010
  5. * Author(s): Steve French (sfrench@us.ibm.com)
  6. *
  7. * This library is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published
  9. * by the Free Software Foundation; either version 2.1 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  15. * the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/fs.h>
  22. #include <linux/stat.h>
  23. #include <linux/slab.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/freezer.h>
  26. #include <linux/sched/signal.h>
  27. #include <linux/wait_bit.h>
  28. #include <linux/fiemap.h>
  29. #include <asm/div64.h>
  30. #include "cifsfs.h"
  31. #include "cifspdu.h"
  32. #include "cifsglob.h"
  33. #include "cifsproto.h"
  34. #include "smb2proto.h"
  35. #include "cifs_debug.h"
  36. #include "cifs_fs_sb.h"
  37. #include "cifs_unicode.h"
  38. #include "fscache.h"
  39. static void cifs_set_ops(struct inode *inode)
  40. {
  41. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  42. switch (inode->i_mode & S_IFMT) {
  43. case S_IFREG:
  44. inode->i_op = &cifs_file_inode_ops;
  45. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
  46. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  47. inode->i_fop = &cifs_file_direct_nobrl_ops;
  48. else
  49. inode->i_fop = &cifs_file_direct_ops;
  50. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
  51. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  52. inode->i_fop = &cifs_file_strict_nobrl_ops;
  53. else
  54. inode->i_fop = &cifs_file_strict_ops;
  55. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
  56. inode->i_fop = &cifs_file_nobrl_ops;
  57. else { /* not direct, send byte range locks */
  58. inode->i_fop = &cifs_file_ops;
  59. }
  60. /* check if server can support readpages */
  61. if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
  62. PAGE_SIZE + MAX_CIFS_HDR_SIZE)
  63. inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
  64. else
  65. inode->i_data.a_ops = &cifs_addr_ops;
  66. break;
  67. case S_IFDIR:
  68. #ifdef CONFIG_CIFS_DFS_UPCALL
  69. if (IS_AUTOMOUNT(inode)) {
  70. inode->i_op = &cifs_dfs_referral_inode_operations;
  71. } else {
  72. #else /* NO DFS support, treat as a directory */
  73. {
  74. #endif
  75. inode->i_op = &cifs_dir_inode_ops;
  76. inode->i_fop = &cifs_dir_ops;
  77. }
  78. break;
  79. case S_IFLNK:
  80. inode->i_op = &cifs_symlink_inode_ops;
  81. break;
  82. default:
  83. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  84. break;
  85. }
  86. }
  87. /* check inode attributes against fattr. If they don't match, tag the
  88. * inode for cache invalidation
  89. */
  90. static void
  91. cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
  92. {
  93. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  94. cifs_dbg(FYI, "%s: revalidating inode %llu\n",
  95. __func__, cifs_i->uniqueid);
  96. if (inode->i_state & I_NEW) {
  97. cifs_dbg(FYI, "%s: inode %llu is new\n",
  98. __func__, cifs_i->uniqueid);
  99. return;
  100. }
  101. /* don't bother with revalidation if we have an oplock */
  102. if (CIFS_CACHE_READ(cifs_i)) {
  103. cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
  104. __func__, cifs_i->uniqueid);
  105. return;
  106. }
  107. /* revalidate if mtime or size have changed */
  108. fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
  109. if (timespec64_equal(&inode->i_mtime, &fattr->cf_mtime) &&
  110. cifs_i->server_eof == fattr->cf_eof) {
  111. cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
  112. __func__, cifs_i->uniqueid);
  113. return;
  114. }
  115. cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
  116. __func__, cifs_i->uniqueid);
  117. set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
  118. }
  119. /*
  120. * copy nlink to the inode, unless it wasn't provided. Provide
  121. * sane values if we don't have an existing one and none was provided
  122. */
  123. static void
  124. cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
  125. {
  126. /*
  127. * if we're in a situation where we can't trust what we
  128. * got from the server (readdir, some non-unix cases)
  129. * fake reasonable values
  130. */
  131. if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
  132. /* only provide fake values on a new inode */
  133. if (inode->i_state & I_NEW) {
  134. if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
  135. set_nlink(inode, 2);
  136. else
  137. set_nlink(inode, 1);
  138. }
  139. return;
  140. }
  141. /* we trust the server, so update it */
  142. set_nlink(inode, fattr->cf_nlink);
  143. }
  144. /* populate an inode with info from a cifs_fattr struct */
  145. void
  146. cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
  147. {
  148. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  149. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  150. cifs_revalidate_cache(inode, fattr);
  151. spin_lock(&inode->i_lock);
  152. fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
  153. fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
  154. fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
  155. /* we do not want atime to be less than mtime, it broke some apps */
  156. if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
  157. inode->i_atime = fattr->cf_mtime;
  158. else
  159. inode->i_atime = fattr->cf_atime;
  160. inode->i_mtime = fattr->cf_mtime;
  161. inode->i_ctime = fattr->cf_ctime;
  162. inode->i_rdev = fattr->cf_rdev;
  163. cifs_nlink_fattr_to_inode(inode, fattr);
  164. inode->i_uid = fattr->cf_uid;
  165. inode->i_gid = fattr->cf_gid;
  166. /* if dynperm is set, don't clobber existing mode */
  167. if (inode->i_state & I_NEW ||
  168. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
  169. inode->i_mode = fattr->cf_mode;
  170. cifs_i->cifsAttrs = fattr->cf_cifsattrs;
  171. if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
  172. cifs_i->time = 0;
  173. else
  174. cifs_i->time = jiffies;
  175. if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
  176. set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
  177. else
  178. clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
  179. cifs_i->server_eof = fattr->cf_eof;
  180. /*
  181. * Can't safely change the file size here if the client is writing to
  182. * it due to potential races.
  183. */
  184. if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
  185. i_size_write(inode, fattr->cf_eof);
  186. /*
  187. * i_blocks is not related to (i_size / i_blksize),
  188. * but instead 512 byte (2**9) size is required for
  189. * calculating num blocks.
  190. */
  191. inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
  192. }
  193. spin_unlock(&inode->i_lock);
  194. if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
  195. inode->i_flags |= S_AUTOMOUNT;
  196. if (inode->i_state & I_NEW)
  197. cifs_set_ops(inode);
  198. }
  199. void
  200. cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
  201. {
  202. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  203. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  204. return;
  205. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  206. }
  207. /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
  208. void
  209. cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
  210. struct cifs_sb_info *cifs_sb)
  211. {
  212. memset(fattr, 0, sizeof(*fattr));
  213. fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
  214. fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
  215. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  216. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  217. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
  218. fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
  219. /* old POSIX extensions don't get create time */
  220. fattr->cf_mode = le64_to_cpu(info->Permissions);
  221. /*
  222. * Since we set the inode type below we need to mask off
  223. * to avoid strange results if bits set above.
  224. */
  225. fattr->cf_mode &= ~S_IFMT;
  226. switch (le32_to_cpu(info->Type)) {
  227. case UNIX_FILE:
  228. fattr->cf_mode |= S_IFREG;
  229. fattr->cf_dtype = DT_REG;
  230. break;
  231. case UNIX_SYMLINK:
  232. fattr->cf_mode |= S_IFLNK;
  233. fattr->cf_dtype = DT_LNK;
  234. break;
  235. case UNIX_DIR:
  236. fattr->cf_mode |= S_IFDIR;
  237. fattr->cf_dtype = DT_DIR;
  238. break;
  239. case UNIX_CHARDEV:
  240. fattr->cf_mode |= S_IFCHR;
  241. fattr->cf_dtype = DT_CHR;
  242. fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  243. le64_to_cpu(info->DevMinor) & MINORMASK);
  244. break;
  245. case UNIX_BLOCKDEV:
  246. fattr->cf_mode |= S_IFBLK;
  247. fattr->cf_dtype = DT_BLK;
  248. fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
  249. le64_to_cpu(info->DevMinor) & MINORMASK);
  250. break;
  251. case UNIX_FIFO:
  252. fattr->cf_mode |= S_IFIFO;
  253. fattr->cf_dtype = DT_FIFO;
  254. break;
  255. case UNIX_SOCKET:
  256. fattr->cf_mode |= S_IFSOCK;
  257. fattr->cf_dtype = DT_SOCK;
  258. break;
  259. default:
  260. /* safest to call it a file if we do not know */
  261. fattr->cf_mode |= S_IFREG;
  262. fattr->cf_dtype = DT_REG;
  263. cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
  264. break;
  265. }
  266. fattr->cf_uid = cifs_sb->mnt_uid;
  267. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
  268. u64 id = le64_to_cpu(info->Uid);
  269. if (id < ((uid_t)-1)) {
  270. kuid_t uid = make_kuid(&init_user_ns, id);
  271. if (uid_valid(uid))
  272. fattr->cf_uid = uid;
  273. }
  274. }
  275. fattr->cf_gid = cifs_sb->mnt_gid;
  276. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
  277. u64 id = le64_to_cpu(info->Gid);
  278. if (id < ((gid_t)-1)) {
  279. kgid_t gid = make_kgid(&init_user_ns, id);
  280. if (gid_valid(gid))
  281. fattr->cf_gid = gid;
  282. }
  283. }
  284. fattr->cf_nlink = le64_to_cpu(info->Nlinks);
  285. }
  286. /*
  287. * Fill a cifs_fattr struct with fake inode info.
  288. *
  289. * Needed to setup cifs_fattr data for the directory which is the
  290. * junction to the new submount (ie to setup the fake directory
  291. * which represents a DFS referral).
  292. */
  293. static void
  294. cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
  295. {
  296. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  297. cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
  298. memset(fattr, 0, sizeof(*fattr));
  299. fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
  300. fattr->cf_uid = cifs_sb->mnt_uid;
  301. fattr->cf_gid = cifs_sb->mnt_gid;
  302. ktime_get_coarse_real_ts64(&fattr->cf_mtime);
  303. fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
  304. fattr->cf_nlink = 2;
  305. fattr->cf_flags = CIFS_FATTR_DFS_REFERRAL;
  306. }
  307. static int
  308. cifs_get_file_info_unix(struct file *filp)
  309. {
  310. int rc;
  311. unsigned int xid;
  312. FILE_UNIX_BASIC_INFO find_data;
  313. struct cifs_fattr fattr;
  314. struct inode *inode = file_inode(filp);
  315. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  316. struct cifsFileInfo *cfile = filp->private_data;
  317. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  318. xid = get_xid();
  319. rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
  320. if (!rc) {
  321. cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
  322. } else if (rc == -EREMOTE) {
  323. cifs_create_dfs_fattr(&fattr, inode->i_sb);
  324. rc = 0;
  325. }
  326. cifs_fattr_to_inode(inode, &fattr);
  327. free_xid(xid);
  328. return rc;
  329. }
  330. int cifs_get_inode_info_unix(struct inode **pinode,
  331. const unsigned char *full_path,
  332. struct super_block *sb, unsigned int xid)
  333. {
  334. int rc;
  335. FILE_UNIX_BASIC_INFO find_data;
  336. struct cifs_fattr fattr;
  337. struct cifs_tcon *tcon;
  338. struct tcon_link *tlink;
  339. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  340. cifs_dbg(FYI, "Getting info on %s\n", full_path);
  341. tlink = cifs_sb_tlink(cifs_sb);
  342. if (IS_ERR(tlink))
  343. return PTR_ERR(tlink);
  344. tcon = tlink_tcon(tlink);
  345. /* could have done a find first instead but this returns more info */
  346. rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
  347. cifs_sb->local_nls, cifs_remap(cifs_sb));
  348. cifs_put_tlink(tlink);
  349. if (!rc) {
  350. cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
  351. } else if (rc == -EREMOTE) {
  352. cifs_create_dfs_fattr(&fattr, sb);
  353. rc = 0;
  354. } else {
  355. return rc;
  356. }
  357. /* check for Minshall+French symlinks */
  358. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
  359. int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
  360. full_path);
  361. if (tmprc)
  362. cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
  363. }
  364. if (*pinode == NULL) {
  365. /* get new inode */
  366. cifs_fill_uniqueid(sb, &fattr);
  367. *pinode = cifs_iget(sb, &fattr);
  368. if (!*pinode)
  369. rc = -ENOMEM;
  370. } else {
  371. /* we already have inode, update it */
  372. /* if uniqueid is different, return error */
  373. if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
  374. CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
  375. CIFS_I(*pinode)->time = 0; /* force reval */
  376. rc = -ESTALE;
  377. goto cgiiu_exit;
  378. }
  379. /* if filetype is different, return error */
  380. if (unlikely(inode_wrong_type(*pinode, fattr.cf_mode))) {
  381. CIFS_I(*pinode)->time = 0; /* force reval */
  382. rc = -ESTALE;
  383. goto cgiiu_exit;
  384. }
  385. cifs_fattr_to_inode(*pinode, &fattr);
  386. }
  387. cgiiu_exit:
  388. return rc;
  389. }
  390. static int
  391. cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
  392. struct cifs_sb_info *cifs_sb, unsigned int xid)
  393. {
  394. int rc;
  395. __u32 oplock;
  396. struct tcon_link *tlink;
  397. struct cifs_tcon *tcon;
  398. struct cifs_fid fid;
  399. struct cifs_open_parms oparms;
  400. struct cifs_io_parms io_parms = {0};
  401. char buf[24];
  402. unsigned int bytes_read;
  403. char *pbuf;
  404. int buf_type = CIFS_NO_BUFFER;
  405. pbuf = buf;
  406. fattr->cf_mode &= ~S_IFMT;
  407. if (fattr->cf_eof == 0) {
  408. fattr->cf_mode |= S_IFIFO;
  409. fattr->cf_dtype = DT_FIFO;
  410. return 0;
  411. } else if (fattr->cf_eof < 8) {
  412. fattr->cf_mode |= S_IFREG;
  413. fattr->cf_dtype = DT_REG;
  414. return -EINVAL; /* EOPNOTSUPP? */
  415. }
  416. tlink = cifs_sb_tlink(cifs_sb);
  417. if (IS_ERR(tlink))
  418. return PTR_ERR(tlink);
  419. tcon = tlink_tcon(tlink);
  420. oparms.tcon = tcon;
  421. oparms.cifs_sb = cifs_sb;
  422. oparms.desired_access = GENERIC_READ;
  423. oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
  424. oparms.disposition = FILE_OPEN;
  425. oparms.path = path;
  426. oparms.fid = &fid;
  427. oparms.reconnect = false;
  428. if (tcon->ses->server->oplocks)
  429. oplock = REQ_OPLOCK;
  430. else
  431. oplock = 0;
  432. rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
  433. if (rc) {
  434. cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
  435. cifs_put_tlink(tlink);
  436. return rc;
  437. }
  438. /* Read header */
  439. io_parms.netfid = fid.netfid;
  440. io_parms.pid = current->tgid;
  441. io_parms.tcon = tcon;
  442. io_parms.offset = 0;
  443. io_parms.length = 24;
  444. rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
  445. &bytes_read, &pbuf, &buf_type);
  446. if ((rc == 0) && (bytes_read >= 8)) {
  447. if (memcmp("IntxBLK", pbuf, 8) == 0) {
  448. cifs_dbg(FYI, "Block device\n");
  449. fattr->cf_mode |= S_IFBLK;
  450. fattr->cf_dtype = DT_BLK;
  451. if (bytes_read == 24) {
  452. /* we have enough to decode dev num */
  453. __u64 mjr; /* major */
  454. __u64 mnr; /* minor */
  455. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  456. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  457. fattr->cf_rdev = MKDEV(mjr, mnr);
  458. }
  459. } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
  460. cifs_dbg(FYI, "Char device\n");
  461. fattr->cf_mode |= S_IFCHR;
  462. fattr->cf_dtype = DT_CHR;
  463. if (bytes_read == 24) {
  464. /* we have enough to decode dev num */
  465. __u64 mjr; /* major */
  466. __u64 mnr; /* minor */
  467. mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
  468. mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
  469. fattr->cf_rdev = MKDEV(mjr, mnr);
  470. }
  471. } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
  472. cifs_dbg(FYI, "Symlink\n");
  473. fattr->cf_mode |= S_IFLNK;
  474. fattr->cf_dtype = DT_LNK;
  475. } else {
  476. fattr->cf_mode |= S_IFREG; /* file? */
  477. fattr->cf_dtype = DT_REG;
  478. rc = -EOPNOTSUPP;
  479. }
  480. } else {
  481. fattr->cf_mode |= S_IFREG; /* then it is a file */
  482. fattr->cf_dtype = DT_REG;
  483. rc = -EOPNOTSUPP; /* or some unknown SFU type */
  484. }
  485. tcon->ses->server->ops->close(xid, tcon, &fid);
  486. cifs_put_tlink(tlink);
  487. return rc;
  488. }
  489. #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
  490. /*
  491. * Fetch mode bits as provided by SFU.
  492. *
  493. * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
  494. */
  495. static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
  496. struct cifs_sb_info *cifs_sb, unsigned int xid)
  497. {
  498. #ifdef CONFIG_CIFS_XATTR
  499. ssize_t rc;
  500. char ea_value[4];
  501. __u32 mode;
  502. struct tcon_link *tlink;
  503. struct cifs_tcon *tcon;
  504. tlink = cifs_sb_tlink(cifs_sb);
  505. if (IS_ERR(tlink))
  506. return PTR_ERR(tlink);
  507. tcon = tlink_tcon(tlink);
  508. if (tcon->ses->server->ops->query_all_EAs == NULL) {
  509. cifs_put_tlink(tlink);
  510. return -EOPNOTSUPP;
  511. }
  512. rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
  513. "SETFILEBITS", ea_value, 4 /* size of buf */,
  514. cifs_sb);
  515. cifs_put_tlink(tlink);
  516. if (rc < 0)
  517. return (int)rc;
  518. else if (rc > 3) {
  519. mode = le32_to_cpu(*((__le32 *)ea_value));
  520. fattr->cf_mode &= ~SFBITS_MASK;
  521. cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
  522. mode, fattr->cf_mode);
  523. fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
  524. cifs_dbg(FYI, "special mode bits 0%o\n", mode);
  525. }
  526. return 0;
  527. #else
  528. return -EOPNOTSUPP;
  529. #endif
  530. }
  531. /* Fill a cifs_fattr struct with info from POSIX info struct */
  532. static void
  533. smb311_posix_info_to_fattr(struct cifs_fattr *fattr, struct smb311_posix_qinfo *info,
  534. struct super_block *sb, bool adjust_tz, bool symlink)
  535. {
  536. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  537. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  538. memset(fattr, 0, sizeof(*fattr));
  539. /* no fattr->flags to set */
  540. fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
  541. fattr->cf_uniqueid = le64_to_cpu(info->Inode);
  542. if (info->LastAccessTime)
  543. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  544. else
  545. ktime_get_coarse_real_ts64(&fattr->cf_atime);
  546. fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
  547. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
  548. if (adjust_tz) {
  549. fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
  550. fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
  551. }
  552. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  553. fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
  554. fattr->cf_createtime = le64_to_cpu(info->CreationTime);
  555. fattr->cf_nlink = le32_to_cpu(info->HardLinks);
  556. fattr->cf_mode = (umode_t) le32_to_cpu(info->Mode);
  557. /* The srv fs device id is overridden on network mount so setting rdev isn't needed here */
  558. /* fattr->cf_rdev = le32_to_cpu(info->DeviceId); */
  559. if (symlink) {
  560. fattr->cf_mode |= S_IFLNK;
  561. fattr->cf_dtype = DT_LNK;
  562. } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
  563. fattr->cf_mode |= S_IFDIR;
  564. fattr->cf_dtype = DT_DIR;
  565. } else { /* file */
  566. fattr->cf_mode |= S_IFREG;
  567. fattr->cf_dtype = DT_REG;
  568. }
  569. /* else if reparse point ... TODO: add support for FIFO and blk dev; special file types */
  570. fattr->cf_uid = cifs_sb->mnt_uid; /* TODO: map uid and gid from SID */
  571. fattr->cf_gid = cifs_sb->mnt_gid;
  572. cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
  573. fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
  574. }
  575. /* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
  576. static void
  577. cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
  578. struct super_block *sb, bool adjust_tz,
  579. bool symlink, u32 reparse_tag)
  580. {
  581. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  582. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  583. memset(fattr, 0, sizeof(*fattr));
  584. fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
  585. if (info->DeletePending)
  586. fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
  587. if (info->LastAccessTime)
  588. fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
  589. else
  590. ktime_get_coarse_real_ts64(&fattr->cf_atime);
  591. fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
  592. fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
  593. if (adjust_tz) {
  594. fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
  595. fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
  596. }
  597. fattr->cf_eof = le64_to_cpu(info->EndOfFile);
  598. fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
  599. fattr->cf_createtime = le64_to_cpu(info->CreationTime);
  600. fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
  601. if (reparse_tag == IO_REPARSE_TAG_LX_SYMLINK) {
  602. fattr->cf_mode |= S_IFLNK | cifs_sb->mnt_file_mode;
  603. fattr->cf_dtype = DT_LNK;
  604. } else if (reparse_tag == IO_REPARSE_TAG_LX_FIFO) {
  605. fattr->cf_mode |= S_IFIFO | cifs_sb->mnt_file_mode;
  606. fattr->cf_dtype = DT_FIFO;
  607. } else if (reparse_tag == IO_REPARSE_TAG_AF_UNIX) {
  608. fattr->cf_mode |= S_IFSOCK | cifs_sb->mnt_file_mode;
  609. fattr->cf_dtype = DT_SOCK;
  610. } else if (reparse_tag == IO_REPARSE_TAG_LX_CHR) {
  611. fattr->cf_mode |= S_IFCHR | cifs_sb->mnt_file_mode;
  612. fattr->cf_dtype = DT_CHR;
  613. } else if (reparse_tag == IO_REPARSE_TAG_LX_BLK) {
  614. fattr->cf_mode |= S_IFBLK | cifs_sb->mnt_file_mode;
  615. fattr->cf_dtype = DT_BLK;
  616. } else if (symlink) { /* TODO add more reparse tag checks */
  617. fattr->cf_mode = S_IFLNK;
  618. fattr->cf_dtype = DT_LNK;
  619. } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
  620. fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
  621. fattr->cf_dtype = DT_DIR;
  622. /*
  623. * Server can return wrong NumberOfLinks value for directories
  624. * when Unix extensions are disabled - fake it.
  625. */
  626. if (!tcon->unix_ext)
  627. fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
  628. } else {
  629. fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
  630. fattr->cf_dtype = DT_REG;
  631. /* clear write bits if ATTR_READONLY is set */
  632. if (fattr->cf_cifsattrs & ATTR_READONLY)
  633. fattr->cf_mode &= ~(S_IWUGO);
  634. /*
  635. * Don't accept zero nlink from non-unix servers unless
  636. * delete is pending. Instead mark it as unknown.
  637. */
  638. if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
  639. !info->DeletePending) {
  640. cifs_dbg(VFS, "bogus file nlink value %u\n",
  641. fattr->cf_nlink);
  642. fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
  643. }
  644. }
  645. fattr->cf_uid = cifs_sb->mnt_uid;
  646. fattr->cf_gid = cifs_sb->mnt_gid;
  647. }
  648. static int
  649. cifs_get_file_info(struct file *filp)
  650. {
  651. int rc;
  652. unsigned int xid;
  653. FILE_ALL_INFO find_data;
  654. struct cifs_fattr fattr;
  655. struct inode *inode = file_inode(filp);
  656. struct cifsFileInfo *cfile = filp->private_data;
  657. struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
  658. struct TCP_Server_Info *server = tcon->ses->server;
  659. if (!server->ops->query_file_info)
  660. return -ENOSYS;
  661. xid = get_xid();
  662. rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
  663. switch (rc) {
  664. case 0:
  665. /* TODO: add support to query reparse tag */
  666. cifs_all_info_to_fattr(&fattr, &find_data, inode->i_sb, false,
  667. false, 0 /* no reparse tag */);
  668. break;
  669. case -EREMOTE:
  670. cifs_create_dfs_fattr(&fattr, inode->i_sb);
  671. rc = 0;
  672. break;
  673. case -EOPNOTSUPP:
  674. case -EINVAL:
  675. /*
  676. * FIXME: legacy server -- fall back to path-based call?
  677. * for now, just skip revalidating and mark inode for
  678. * immediate reval.
  679. */
  680. rc = 0;
  681. CIFS_I(inode)->time = 0;
  682. default:
  683. goto cgfi_exit;
  684. }
  685. /*
  686. * don't bother with SFU junk here -- just mark inode as needing
  687. * revalidation.
  688. */
  689. fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
  690. fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
  691. cifs_fattr_to_inode(inode, &fattr);
  692. cgfi_exit:
  693. free_xid(xid);
  694. return rc;
  695. }
  696. /* Simple function to return a 64 bit hash of string. Rarely called */
  697. static __u64 simple_hashstr(const char *str)
  698. {
  699. const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
  700. __u64 hash = 0;
  701. while (*str)
  702. hash = (hash + (__u64) *str++) * hash_mult;
  703. return hash;
  704. }
  705. /**
  706. * cifs_backup_query_path_info - SMB1 fallback code to get ino
  707. *
  708. * Fallback code to get file metadata when we don't have access to
  709. * @full_path (EACCES) and have backup creds.
  710. *
  711. * @data will be set to search info result buffer
  712. * @resp_buf will be set to cifs resp buf and needs to be freed with
  713. * cifs_buf_release() when done with @data.
  714. */
  715. static int
  716. cifs_backup_query_path_info(int xid,
  717. struct cifs_tcon *tcon,
  718. struct super_block *sb,
  719. const char *full_path,
  720. void **resp_buf,
  721. FILE_ALL_INFO **data)
  722. {
  723. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  724. struct cifs_search_info info = {0};
  725. u16 flags;
  726. int rc;
  727. *resp_buf = NULL;
  728. info.endOfSearch = false;
  729. if (tcon->unix_ext)
  730. info.info_level = SMB_FIND_FILE_UNIX;
  731. else if ((tcon->ses->capabilities &
  732. tcon->ses->server->vals->cap_nt_find) == 0)
  733. info.info_level = SMB_FIND_FILE_INFO_STANDARD;
  734. else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
  735. info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
  736. else /* no srvino useful for fallback to some netapp */
  737. info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
  738. flags = CIFS_SEARCH_CLOSE_ALWAYS |
  739. CIFS_SEARCH_CLOSE_AT_END |
  740. CIFS_SEARCH_BACKUP_SEARCH;
  741. rc = CIFSFindFirst(xid, tcon, full_path,
  742. cifs_sb, NULL, flags, &info, false);
  743. if (rc)
  744. return rc;
  745. *resp_buf = (void *)info.ntwrk_buf_start;
  746. *data = (FILE_ALL_INFO *)info.srch_entries_start;
  747. return 0;
  748. }
  749. static void
  750. cifs_set_fattr_ino(int xid,
  751. struct cifs_tcon *tcon,
  752. struct super_block *sb,
  753. struct inode **inode,
  754. const char *full_path,
  755. FILE_ALL_INFO *data,
  756. struct cifs_fattr *fattr)
  757. {
  758. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  759. struct TCP_Server_Info *server = tcon->ses->server;
  760. int rc;
  761. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
  762. if (*inode)
  763. fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
  764. else
  765. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  766. return;
  767. }
  768. /*
  769. * If we have an inode pass a NULL tcon to ensure we don't
  770. * make a round trip to the server. This only works for SMB2+.
  771. */
  772. rc = server->ops->get_srv_inum(xid,
  773. *inode ? NULL : tcon,
  774. cifs_sb, full_path,
  775. &fattr->cf_uniqueid,
  776. data);
  777. if (rc) {
  778. /*
  779. * If that fails reuse existing ino or generate one
  780. * and disable server ones
  781. */
  782. if (*inode)
  783. fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
  784. else {
  785. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  786. cifs_autodisable_serverino(cifs_sb);
  787. }
  788. return;
  789. }
  790. /* If no errors, check for zero root inode (invalid) */
  791. if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
  792. cifs_dbg(FYI, "Invalid (0) inodenum\n");
  793. if (*inode) {
  794. /* reuse */
  795. fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
  796. } else {
  797. /* make an ino by hashing the UNC */
  798. fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
  799. fattr->cf_uniqueid = simple_hashstr(tcon->treeName);
  800. }
  801. }
  802. }
  803. static inline bool is_inode_cache_good(struct inode *ino)
  804. {
  805. return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
  806. }
  807. int
  808. cifs_get_inode_info(struct inode **inode,
  809. const char *full_path,
  810. FILE_ALL_INFO *in_data,
  811. struct super_block *sb, int xid,
  812. const struct cifs_fid *fid)
  813. {
  814. struct cifs_tcon *tcon;
  815. struct TCP_Server_Info *server;
  816. struct tcon_link *tlink;
  817. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  818. bool adjust_tz = false;
  819. struct cifs_fattr fattr = {0};
  820. bool is_reparse_point = false;
  821. FILE_ALL_INFO *data = in_data;
  822. FILE_ALL_INFO *tmp_data = NULL;
  823. void *smb1_backup_rsp_buf = NULL;
  824. int rc = 0;
  825. int tmprc = 0;
  826. __u32 reparse_tag = 0;
  827. tlink = cifs_sb_tlink(cifs_sb);
  828. if (IS_ERR(tlink))
  829. return PTR_ERR(tlink);
  830. tcon = tlink_tcon(tlink);
  831. server = tcon->ses->server;
  832. /*
  833. * 1. Fetch file metadata if not provided (data)
  834. */
  835. if (!data) {
  836. if (is_inode_cache_good(*inode)) {
  837. cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
  838. goto out;
  839. }
  840. tmp_data = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
  841. if (!tmp_data) {
  842. rc = -ENOMEM;
  843. goto out;
  844. }
  845. rc = server->ops->query_path_info(xid, tcon, cifs_sb,
  846. full_path, tmp_data,
  847. &adjust_tz, &is_reparse_point);
  848. data = tmp_data;
  849. }
  850. /*
  851. * 2. Convert it to internal cifs metadata (fattr)
  852. */
  853. switch (rc) {
  854. case 0:
  855. /*
  856. * If the file is a reparse point, it is more complicated
  857. * since we have to check if its reparse tag matches a known
  858. * special file type e.g. symlink or fifo or char etc.
  859. */
  860. if ((le32_to_cpu(data->Attributes) & ATTR_REPARSE) &&
  861. server->ops->query_reparse_tag) {
  862. rc = server->ops->query_reparse_tag(xid, tcon, cifs_sb,
  863. full_path, &reparse_tag);
  864. cifs_dbg(FYI, "reparse tag 0x%x\n", reparse_tag);
  865. }
  866. cifs_all_info_to_fattr(&fattr, data, sb, adjust_tz,
  867. is_reparse_point, reparse_tag);
  868. break;
  869. case -EREMOTE:
  870. /* DFS link, no metadata available on this server */
  871. cifs_create_dfs_fattr(&fattr, sb);
  872. rc = 0;
  873. break;
  874. case -EACCES:
  875. /*
  876. * perm errors, try again with backup flags if possible
  877. *
  878. * For SMB2 and later the backup intent flag
  879. * is already sent if needed on open and there
  880. * is no path based FindFirst operation to use
  881. * to retry with
  882. */
  883. if (backup_cred(cifs_sb) && is_smb1_server(server)) {
  884. /* for easier reading */
  885. FILE_DIRECTORY_INFO *fdi;
  886. SEARCH_ID_FULL_DIR_INFO *si;
  887. rc = cifs_backup_query_path_info(xid, tcon, sb,
  888. full_path,
  889. &smb1_backup_rsp_buf,
  890. &data);
  891. if (rc)
  892. goto out;
  893. fdi = (FILE_DIRECTORY_INFO *)data;
  894. si = (SEARCH_ID_FULL_DIR_INFO *)data;
  895. cifs_dir_info_to_fattr(&fattr, fdi, cifs_sb);
  896. fattr.cf_uniqueid = le64_to_cpu(si->UniqueId);
  897. /* uniqueid set, skip get inum step */
  898. goto handle_mnt_opt;
  899. } else {
  900. /* nothing we can do, bail out */
  901. goto out;
  902. }
  903. break;
  904. default:
  905. cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
  906. goto out;
  907. }
  908. /*
  909. * 3. Get or update inode number (fattr.cf_uniqueid)
  910. */
  911. cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, &fattr);
  912. /*
  913. * 4. Tweak fattr based on mount options
  914. */
  915. handle_mnt_opt:
  916. /* query for SFU type info if supported and needed */
  917. if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
  918. cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
  919. tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
  920. if (tmprc)
  921. cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
  922. }
  923. /* fill in 0777 bits from ACL */
  924. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
  925. rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, true,
  926. full_path, fid);
  927. if (rc == -EREMOTE)
  928. rc = 0;
  929. if (rc) {
  930. cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
  931. __func__, rc);
  932. goto out;
  933. }
  934. } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
  935. rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, false,
  936. full_path, fid);
  937. if (rc == -EREMOTE)
  938. rc = 0;
  939. if (rc) {
  940. cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
  941. __func__, rc);
  942. goto out;
  943. }
  944. }
  945. /* fill in remaining high mode bits e.g. SUID, VTX */
  946. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
  947. cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
  948. /* check for Minshall+French symlinks */
  949. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
  950. tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
  951. full_path);
  952. if (tmprc)
  953. cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
  954. }
  955. /*
  956. * 5. Update inode with final fattr data
  957. */
  958. if (!*inode) {
  959. *inode = cifs_iget(sb, &fattr);
  960. if (!*inode)
  961. rc = -ENOMEM;
  962. } else {
  963. /* we already have inode, update it */
  964. /* if uniqueid is different, return error */
  965. if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
  966. CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
  967. CIFS_I(*inode)->time = 0; /* force reval */
  968. rc = -ESTALE;
  969. goto out;
  970. }
  971. /* if filetype is different, return error */
  972. if (unlikely(((*inode)->i_mode & S_IFMT) !=
  973. (fattr.cf_mode & S_IFMT))) {
  974. CIFS_I(*inode)->time = 0; /* force reval */
  975. rc = -ESTALE;
  976. goto out;
  977. }
  978. cifs_fattr_to_inode(*inode, &fattr);
  979. }
  980. out:
  981. cifs_buf_release(smb1_backup_rsp_buf);
  982. cifs_put_tlink(tlink);
  983. kfree(tmp_data);
  984. return rc;
  985. }
  986. int
  987. smb311_posix_get_inode_info(struct inode **inode,
  988. const char *full_path,
  989. struct super_block *sb, unsigned int xid)
  990. {
  991. struct cifs_tcon *tcon;
  992. struct tcon_link *tlink;
  993. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  994. bool adjust_tz = false;
  995. struct cifs_fattr fattr = {0};
  996. bool symlink = false;
  997. struct smb311_posix_qinfo *data = NULL;
  998. int rc = 0;
  999. int tmprc = 0;
  1000. tlink = cifs_sb_tlink(cifs_sb);
  1001. if (IS_ERR(tlink))
  1002. return PTR_ERR(tlink);
  1003. tcon = tlink_tcon(tlink);
  1004. /*
  1005. * 1. Fetch file metadata
  1006. */
  1007. if (is_inode_cache_good(*inode)) {
  1008. cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
  1009. goto out;
  1010. }
  1011. data = kmalloc(sizeof(struct smb311_posix_qinfo), GFP_KERNEL);
  1012. if (!data) {
  1013. rc = -ENOMEM;
  1014. goto out;
  1015. }
  1016. rc = smb311_posix_query_path_info(xid, tcon, cifs_sb,
  1017. full_path, data,
  1018. &adjust_tz, &symlink);
  1019. /*
  1020. * 2. Convert it to internal cifs metadata (fattr)
  1021. */
  1022. switch (rc) {
  1023. case 0:
  1024. smb311_posix_info_to_fattr(&fattr, data, sb, adjust_tz, symlink);
  1025. break;
  1026. case -EREMOTE:
  1027. /* DFS link, no metadata available on this server */
  1028. cifs_create_dfs_fattr(&fattr, sb);
  1029. rc = 0;
  1030. break;
  1031. case -EACCES:
  1032. /*
  1033. * For SMB2 and later the backup intent flag
  1034. * is already sent if needed on open and there
  1035. * is no path based FindFirst operation to use
  1036. * to retry with so nothing we can do, bail out
  1037. */
  1038. goto out;
  1039. default:
  1040. cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
  1041. goto out;
  1042. }
  1043. /*
  1044. * 3. Tweak fattr based on mount options
  1045. */
  1046. /* check for Minshall+French symlinks */
  1047. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
  1048. tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
  1049. full_path);
  1050. if (tmprc)
  1051. cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
  1052. }
  1053. /*
  1054. * 4. Update inode with final fattr data
  1055. */
  1056. if (!*inode) {
  1057. *inode = cifs_iget(sb, &fattr);
  1058. if (!*inode)
  1059. rc = -ENOMEM;
  1060. } else {
  1061. /* we already have inode, update it */
  1062. /* if uniqueid is different, return error */
  1063. if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
  1064. CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
  1065. CIFS_I(*inode)->time = 0; /* force reval */
  1066. rc = -ESTALE;
  1067. goto out;
  1068. }
  1069. /* if filetype is different, return error */
  1070. if (unlikely(((*inode)->i_mode & S_IFMT) !=
  1071. (fattr.cf_mode & S_IFMT))) {
  1072. CIFS_I(*inode)->time = 0; /* force reval */
  1073. rc = -ESTALE;
  1074. goto out;
  1075. }
  1076. cifs_fattr_to_inode(*inode, &fattr);
  1077. }
  1078. out:
  1079. cifs_put_tlink(tlink);
  1080. kfree(data);
  1081. return rc;
  1082. }
  1083. static const struct inode_operations cifs_ipc_inode_ops = {
  1084. .lookup = cifs_lookup,
  1085. };
  1086. static int
  1087. cifs_find_inode(struct inode *inode, void *opaque)
  1088. {
  1089. struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
  1090. /* don't match inode with different uniqueid */
  1091. if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
  1092. return 0;
  1093. /* use createtime like an i_generation field */
  1094. if (CIFS_I(inode)->createtime != fattr->cf_createtime)
  1095. return 0;
  1096. /* don't match inode of different type */
  1097. if (inode_wrong_type(inode, fattr->cf_mode))
  1098. return 0;
  1099. /* if it's not a directory or has no dentries, then flag it */
  1100. if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
  1101. fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
  1102. return 1;
  1103. }
  1104. static int
  1105. cifs_init_inode(struct inode *inode, void *opaque)
  1106. {
  1107. struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
  1108. CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
  1109. CIFS_I(inode)->createtime = fattr->cf_createtime;
  1110. return 0;
  1111. }
  1112. /*
  1113. * walk dentry list for an inode and report whether it has aliases that
  1114. * are hashed. We use this to determine if a directory inode can actually
  1115. * be used.
  1116. */
  1117. static bool
  1118. inode_has_hashed_dentries(struct inode *inode)
  1119. {
  1120. struct dentry *dentry;
  1121. spin_lock(&inode->i_lock);
  1122. hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
  1123. if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
  1124. spin_unlock(&inode->i_lock);
  1125. return true;
  1126. }
  1127. }
  1128. spin_unlock(&inode->i_lock);
  1129. return false;
  1130. }
  1131. /* Given fattrs, get a corresponding inode */
  1132. struct inode *
  1133. cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
  1134. {
  1135. unsigned long hash;
  1136. struct inode *inode;
  1137. retry_iget5_locked:
  1138. cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
  1139. /* hash down to 32-bits on 32-bit arch */
  1140. hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
  1141. inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
  1142. if (inode) {
  1143. /* was there a potentially problematic inode collision? */
  1144. if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
  1145. fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
  1146. if (inode_has_hashed_dentries(inode)) {
  1147. cifs_autodisable_serverino(CIFS_SB(sb));
  1148. iput(inode);
  1149. fattr->cf_uniqueid = iunique(sb, ROOT_I);
  1150. goto retry_iget5_locked;
  1151. }
  1152. }
  1153. cifs_fattr_to_inode(inode, fattr);
  1154. if (sb->s_flags & SB_NOATIME)
  1155. inode->i_flags |= S_NOATIME | S_NOCMTIME;
  1156. if (inode->i_state & I_NEW) {
  1157. inode->i_ino = hash;
  1158. #ifdef CONFIG_CIFS_FSCACHE
  1159. /* initialize per-inode cache cookie pointer */
  1160. CIFS_I(inode)->fscache = NULL;
  1161. #endif
  1162. unlock_new_inode(inode);
  1163. }
  1164. }
  1165. return inode;
  1166. }
  1167. /* gets root inode */
  1168. struct inode *cifs_root_iget(struct super_block *sb)
  1169. {
  1170. unsigned int xid;
  1171. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  1172. struct inode *inode = NULL;
  1173. long rc;
  1174. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  1175. char *path = NULL;
  1176. int len;
  1177. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
  1178. && cifs_sb->prepath) {
  1179. len = strlen(cifs_sb->prepath);
  1180. path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
  1181. if (path == NULL)
  1182. return ERR_PTR(-ENOMEM);
  1183. path[0] = '/';
  1184. memcpy(path+1, cifs_sb->prepath, len);
  1185. } else {
  1186. path = kstrdup("", GFP_KERNEL);
  1187. if (path == NULL)
  1188. return ERR_PTR(-ENOMEM);
  1189. }
  1190. xid = get_xid();
  1191. if (tcon->unix_ext) {
  1192. rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
  1193. /* some servers mistakenly claim POSIX support */
  1194. if (rc != -EOPNOTSUPP)
  1195. goto iget_no_retry;
  1196. cifs_dbg(VFS, "server does not support POSIX extensions\n");
  1197. tcon->unix_ext = false;
  1198. }
  1199. convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
  1200. if (tcon->posix_extensions)
  1201. rc = smb311_posix_get_inode_info(&inode, path, sb, xid);
  1202. else
  1203. rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
  1204. iget_no_retry:
  1205. if (!inode) {
  1206. inode = ERR_PTR(rc);
  1207. goto out;
  1208. }
  1209. #ifdef CONFIG_CIFS_FSCACHE
  1210. /* populate tcon->resource_id */
  1211. tcon->resource_id = CIFS_I(inode)->uniqueid;
  1212. #endif
  1213. if (rc && tcon->pipe) {
  1214. cifs_dbg(FYI, "ipc connection - fake read inode\n");
  1215. spin_lock(&inode->i_lock);
  1216. inode->i_mode |= S_IFDIR;
  1217. set_nlink(inode, 2);
  1218. inode->i_op = &cifs_ipc_inode_ops;
  1219. inode->i_fop = &simple_dir_operations;
  1220. inode->i_uid = cifs_sb->mnt_uid;
  1221. inode->i_gid = cifs_sb->mnt_gid;
  1222. spin_unlock(&inode->i_lock);
  1223. } else if (rc) {
  1224. iget_failed(inode);
  1225. inode = ERR_PTR(rc);
  1226. }
  1227. out:
  1228. kfree(path);
  1229. free_xid(xid);
  1230. return inode;
  1231. }
  1232. int
  1233. cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
  1234. char *full_path, __u32 dosattr)
  1235. {
  1236. bool set_time = false;
  1237. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1238. struct TCP_Server_Info *server;
  1239. FILE_BASIC_INFO info_buf;
  1240. if (attrs == NULL)
  1241. return -EINVAL;
  1242. server = cifs_sb_master_tcon(cifs_sb)->ses->server;
  1243. if (!server->ops->set_file_info)
  1244. return -ENOSYS;
  1245. info_buf.Pad = 0;
  1246. if (attrs->ia_valid & ATTR_ATIME) {
  1247. set_time = true;
  1248. info_buf.LastAccessTime =
  1249. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
  1250. } else
  1251. info_buf.LastAccessTime = 0;
  1252. if (attrs->ia_valid & ATTR_MTIME) {
  1253. set_time = true;
  1254. info_buf.LastWriteTime =
  1255. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
  1256. } else
  1257. info_buf.LastWriteTime = 0;
  1258. /*
  1259. * Samba throws this field away, but windows may actually use it.
  1260. * Do not set ctime unless other time stamps are changed explicitly
  1261. * (i.e. by utimes()) since we would then have a mix of client and
  1262. * server times.
  1263. */
  1264. if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
  1265. cifs_dbg(FYI, "CIFS - CTIME changed\n");
  1266. info_buf.ChangeTime =
  1267. cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
  1268. } else
  1269. info_buf.ChangeTime = 0;
  1270. info_buf.CreationTime = 0; /* don't change */
  1271. info_buf.Attributes = cpu_to_le32(dosattr);
  1272. return server->ops->set_file_info(inode, full_path, &info_buf, xid);
  1273. }
  1274. /*
  1275. * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
  1276. * and rename it to a random name that hopefully won't conflict with
  1277. * anything else.
  1278. */
  1279. int
  1280. cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
  1281. const unsigned int xid)
  1282. {
  1283. int oplock = 0;
  1284. int rc;
  1285. struct cifs_fid fid;
  1286. struct cifs_open_parms oparms;
  1287. struct inode *inode = d_inode(dentry);
  1288. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  1289. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1290. struct tcon_link *tlink;
  1291. struct cifs_tcon *tcon;
  1292. __u32 dosattr, origattr;
  1293. FILE_BASIC_INFO *info_buf = NULL;
  1294. tlink = cifs_sb_tlink(cifs_sb);
  1295. if (IS_ERR(tlink))
  1296. return PTR_ERR(tlink);
  1297. tcon = tlink_tcon(tlink);
  1298. /*
  1299. * We cannot rename the file if the server doesn't support
  1300. * CAP_INFOLEVEL_PASSTHRU
  1301. */
  1302. if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
  1303. rc = -EBUSY;
  1304. goto out;
  1305. }
  1306. oparms.tcon = tcon;
  1307. oparms.cifs_sb = cifs_sb;
  1308. oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
  1309. oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
  1310. oparms.disposition = FILE_OPEN;
  1311. oparms.path = full_path;
  1312. oparms.fid = &fid;
  1313. oparms.reconnect = false;
  1314. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  1315. if (rc != 0)
  1316. goto out;
  1317. origattr = cifsInode->cifsAttrs;
  1318. if (origattr == 0)
  1319. origattr |= ATTR_NORMAL;
  1320. dosattr = origattr & ~ATTR_READONLY;
  1321. if (dosattr == 0)
  1322. dosattr |= ATTR_NORMAL;
  1323. dosattr |= ATTR_HIDDEN;
  1324. /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
  1325. if (dosattr != origattr) {
  1326. info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
  1327. if (info_buf == NULL) {
  1328. rc = -ENOMEM;
  1329. goto out_close;
  1330. }
  1331. info_buf->Attributes = cpu_to_le32(dosattr);
  1332. rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
  1333. current->tgid);
  1334. /* although we would like to mark the file hidden
  1335. if that fails we will still try to rename it */
  1336. if (!rc)
  1337. cifsInode->cifsAttrs = dosattr;
  1338. else
  1339. dosattr = origattr; /* since not able to change them */
  1340. }
  1341. /* rename the file */
  1342. rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
  1343. cifs_sb->local_nls,
  1344. cifs_remap(cifs_sb));
  1345. if (rc != 0) {
  1346. rc = -EBUSY;
  1347. goto undo_setattr;
  1348. }
  1349. /* try to set DELETE_ON_CLOSE */
  1350. if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
  1351. rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
  1352. current->tgid);
  1353. /*
  1354. * some samba versions return -ENOENT when we try to set the
  1355. * file disposition here. Likely a samba bug, but work around
  1356. * it for now. This means that some cifsXXX files may hang
  1357. * around after they shouldn't.
  1358. *
  1359. * BB: remove this hack after more servers have the fix
  1360. */
  1361. if (rc == -ENOENT)
  1362. rc = 0;
  1363. else if (rc != 0) {
  1364. rc = -EBUSY;
  1365. goto undo_rename;
  1366. }
  1367. set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
  1368. }
  1369. out_close:
  1370. CIFSSMBClose(xid, tcon, fid.netfid);
  1371. out:
  1372. kfree(info_buf);
  1373. cifs_put_tlink(tlink);
  1374. return rc;
  1375. /*
  1376. * reset everything back to the original state. Don't bother
  1377. * dealing with errors here since we can't do anything about
  1378. * them anyway.
  1379. */
  1380. undo_rename:
  1381. CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
  1382. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1383. undo_setattr:
  1384. if (dosattr != origattr) {
  1385. info_buf->Attributes = cpu_to_le32(origattr);
  1386. if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
  1387. current->tgid))
  1388. cifsInode->cifsAttrs = origattr;
  1389. }
  1390. goto out_close;
  1391. }
  1392. /* copied from fs/nfs/dir.c with small changes */
  1393. static void
  1394. cifs_drop_nlink(struct inode *inode)
  1395. {
  1396. spin_lock(&inode->i_lock);
  1397. if (inode->i_nlink > 0)
  1398. drop_nlink(inode);
  1399. spin_unlock(&inode->i_lock);
  1400. }
  1401. /*
  1402. * If d_inode(dentry) is null (usually meaning the cached dentry
  1403. * is a negative dentry) then we would attempt a standard SMB delete, but
  1404. * if that fails we can not attempt the fall back mechanisms on EACCES
  1405. * but will return the EACCES to the caller. Note that the VFS does not call
  1406. * unlink on negative dentries currently.
  1407. */
  1408. int cifs_unlink(struct inode *dir, struct dentry *dentry)
  1409. {
  1410. int rc = 0;
  1411. unsigned int xid;
  1412. char *full_path = NULL;
  1413. struct inode *inode = d_inode(dentry);
  1414. struct cifsInodeInfo *cifs_inode;
  1415. struct super_block *sb = dir->i_sb;
  1416. struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
  1417. struct tcon_link *tlink;
  1418. struct cifs_tcon *tcon;
  1419. struct TCP_Server_Info *server;
  1420. struct iattr *attrs = NULL;
  1421. __u32 dosattr = 0, origattr = 0;
  1422. cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
  1423. tlink = cifs_sb_tlink(cifs_sb);
  1424. if (IS_ERR(tlink))
  1425. return PTR_ERR(tlink);
  1426. tcon = tlink_tcon(tlink);
  1427. server = tcon->ses->server;
  1428. xid = get_xid();
  1429. if (tcon->nodelete) {
  1430. rc = -EACCES;
  1431. goto unlink_out;
  1432. }
  1433. /* Unlink can be called from rename so we can not take the
  1434. * sb->s_vfs_rename_mutex here */
  1435. full_path = build_path_from_dentry(dentry);
  1436. if (full_path == NULL) {
  1437. rc = -ENOMEM;
  1438. goto unlink_out;
  1439. }
  1440. if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  1441. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  1442. rc = CIFSPOSIXDelFile(xid, tcon, full_path,
  1443. SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
  1444. cifs_remap(cifs_sb));
  1445. cifs_dbg(FYI, "posix del rc %d\n", rc);
  1446. if ((rc == 0) || (rc == -ENOENT))
  1447. goto psx_del_no_retry;
  1448. }
  1449. retry_std_delete:
  1450. if (!server->ops->unlink) {
  1451. rc = -ENOSYS;
  1452. goto psx_del_no_retry;
  1453. }
  1454. rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
  1455. psx_del_no_retry:
  1456. if (!rc) {
  1457. if (inode)
  1458. cifs_drop_nlink(inode);
  1459. } else if (rc == -ENOENT) {
  1460. d_drop(dentry);
  1461. } else if (rc == -EBUSY) {
  1462. if (server->ops->rename_pending_delete) {
  1463. rc = server->ops->rename_pending_delete(full_path,
  1464. dentry, xid);
  1465. if (rc == 0)
  1466. cifs_drop_nlink(inode);
  1467. }
  1468. } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
  1469. attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
  1470. if (attrs == NULL) {
  1471. rc = -ENOMEM;
  1472. goto out_reval;
  1473. }
  1474. /* try to reset dos attributes */
  1475. cifs_inode = CIFS_I(inode);
  1476. origattr = cifs_inode->cifsAttrs;
  1477. if (origattr == 0)
  1478. origattr |= ATTR_NORMAL;
  1479. dosattr = origattr & ~ATTR_READONLY;
  1480. if (dosattr == 0)
  1481. dosattr |= ATTR_NORMAL;
  1482. dosattr |= ATTR_HIDDEN;
  1483. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  1484. if (rc != 0)
  1485. goto out_reval;
  1486. goto retry_std_delete;
  1487. }
  1488. /* undo the setattr if we errored out and it's needed */
  1489. if (rc != 0 && dosattr != 0)
  1490. cifs_set_file_info(inode, attrs, xid, full_path, origattr);
  1491. out_reval:
  1492. if (inode) {
  1493. cifs_inode = CIFS_I(inode);
  1494. cifs_inode->time = 0; /* will force revalidate to get info
  1495. when needed */
  1496. inode->i_ctime = current_time(inode);
  1497. }
  1498. dir->i_ctime = dir->i_mtime = current_time(dir);
  1499. cifs_inode = CIFS_I(dir);
  1500. CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
  1501. unlink_out:
  1502. kfree(full_path);
  1503. kfree(attrs);
  1504. free_xid(xid);
  1505. cifs_put_tlink(tlink);
  1506. return rc;
  1507. }
  1508. static int
  1509. cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
  1510. const char *full_path, struct cifs_sb_info *cifs_sb,
  1511. struct cifs_tcon *tcon, const unsigned int xid)
  1512. {
  1513. int rc = 0;
  1514. struct inode *inode = NULL;
  1515. if (tcon->posix_extensions)
  1516. rc = smb311_posix_get_inode_info(&inode, full_path, parent->i_sb, xid);
  1517. else if (tcon->unix_ext)
  1518. rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
  1519. xid);
  1520. else
  1521. rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
  1522. xid, NULL);
  1523. if (rc)
  1524. return rc;
  1525. /*
  1526. * setting nlink not necessary except in cases where we failed to get it
  1527. * from the server or was set bogus. Also, since this is a brand new
  1528. * inode, no need to grab the i_lock before setting the i_nlink.
  1529. */
  1530. if (inode->i_nlink < 2)
  1531. set_nlink(inode, 2);
  1532. mode &= ~current_umask();
  1533. /* must turn on setgid bit if parent dir has it */
  1534. if (parent->i_mode & S_ISGID)
  1535. mode |= S_ISGID;
  1536. if (tcon->unix_ext) {
  1537. struct cifs_unix_set_info_args args = {
  1538. .mode = mode,
  1539. .ctime = NO_CHANGE_64,
  1540. .atime = NO_CHANGE_64,
  1541. .mtime = NO_CHANGE_64,
  1542. .device = 0,
  1543. };
  1544. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1545. args.uid = current_fsuid();
  1546. if (parent->i_mode & S_ISGID)
  1547. args.gid = parent->i_gid;
  1548. else
  1549. args.gid = current_fsgid();
  1550. } else {
  1551. args.uid = INVALID_UID; /* no change */
  1552. args.gid = INVALID_GID; /* no change */
  1553. }
  1554. CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
  1555. cifs_sb->local_nls,
  1556. cifs_remap(cifs_sb));
  1557. } else {
  1558. struct TCP_Server_Info *server = tcon->ses->server;
  1559. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  1560. (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
  1561. server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
  1562. tcon, xid);
  1563. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
  1564. inode->i_mode = (mode | S_IFDIR);
  1565. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
  1566. inode->i_uid = current_fsuid();
  1567. if (inode->i_mode & S_ISGID)
  1568. inode->i_gid = parent->i_gid;
  1569. else
  1570. inode->i_gid = current_fsgid();
  1571. }
  1572. }
  1573. d_instantiate(dentry, inode);
  1574. return rc;
  1575. }
  1576. static int
  1577. cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
  1578. const char *full_path, struct cifs_sb_info *cifs_sb,
  1579. struct cifs_tcon *tcon, const unsigned int xid)
  1580. {
  1581. int rc = 0;
  1582. u32 oplock = 0;
  1583. FILE_UNIX_BASIC_INFO *info = NULL;
  1584. struct inode *newinode = NULL;
  1585. struct cifs_fattr fattr;
  1586. info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
  1587. if (info == NULL) {
  1588. rc = -ENOMEM;
  1589. goto posix_mkdir_out;
  1590. }
  1591. mode &= ~current_umask();
  1592. rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
  1593. NULL /* netfid */, info, &oplock, full_path,
  1594. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1595. if (rc == -EOPNOTSUPP)
  1596. goto posix_mkdir_out;
  1597. else if (rc) {
  1598. cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
  1599. d_drop(dentry);
  1600. goto posix_mkdir_out;
  1601. }
  1602. if (info->Type == cpu_to_le32(-1))
  1603. /* no return info, go query for it */
  1604. goto posix_mkdir_get_info;
  1605. /*
  1606. * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
  1607. * need to set uid/gid.
  1608. */
  1609. cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
  1610. cifs_fill_uniqueid(inode->i_sb, &fattr);
  1611. newinode = cifs_iget(inode->i_sb, &fattr);
  1612. if (!newinode)
  1613. goto posix_mkdir_get_info;
  1614. d_instantiate(dentry, newinode);
  1615. #ifdef CONFIG_CIFS_DEBUG2
  1616. cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
  1617. dentry, dentry, newinode);
  1618. if (newinode->i_nlink != 2)
  1619. cifs_dbg(FYI, "unexpected number of links %d\n",
  1620. newinode->i_nlink);
  1621. #endif
  1622. posix_mkdir_out:
  1623. kfree(info);
  1624. return rc;
  1625. posix_mkdir_get_info:
  1626. rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
  1627. xid);
  1628. goto posix_mkdir_out;
  1629. }
  1630. int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
  1631. {
  1632. int rc = 0;
  1633. unsigned int xid;
  1634. struct cifs_sb_info *cifs_sb;
  1635. struct tcon_link *tlink;
  1636. struct cifs_tcon *tcon;
  1637. struct TCP_Server_Info *server;
  1638. char *full_path;
  1639. cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
  1640. mode, inode);
  1641. cifs_sb = CIFS_SB(inode->i_sb);
  1642. tlink = cifs_sb_tlink(cifs_sb);
  1643. if (IS_ERR(tlink))
  1644. return PTR_ERR(tlink);
  1645. tcon = tlink_tcon(tlink);
  1646. xid = get_xid();
  1647. full_path = build_path_from_dentry(direntry);
  1648. if (full_path == NULL) {
  1649. rc = -ENOMEM;
  1650. goto mkdir_out;
  1651. }
  1652. server = tcon->ses->server;
  1653. if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
  1654. rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
  1655. cifs_sb);
  1656. d_drop(direntry); /* for time being always refresh inode info */
  1657. goto mkdir_out;
  1658. }
  1659. if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
  1660. le64_to_cpu(tcon->fsUnixInfo.Capability))) {
  1661. rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
  1662. tcon, xid);
  1663. if (rc != -EOPNOTSUPP)
  1664. goto mkdir_out;
  1665. }
  1666. if (!server->ops->mkdir) {
  1667. rc = -ENOSYS;
  1668. goto mkdir_out;
  1669. }
  1670. /* BB add setting the equivalent of mode via CreateX w/ACLs */
  1671. rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
  1672. if (rc) {
  1673. cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
  1674. d_drop(direntry);
  1675. goto mkdir_out;
  1676. }
  1677. /* TODO: skip this for smb2/smb3 */
  1678. rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
  1679. xid);
  1680. mkdir_out:
  1681. /*
  1682. * Force revalidate to get parent dir info when needed since cached
  1683. * attributes are invalid now.
  1684. */
  1685. CIFS_I(inode)->time = 0;
  1686. kfree(full_path);
  1687. free_xid(xid);
  1688. cifs_put_tlink(tlink);
  1689. return rc;
  1690. }
  1691. int cifs_rmdir(struct inode *inode, struct dentry *direntry)
  1692. {
  1693. int rc = 0;
  1694. unsigned int xid;
  1695. struct cifs_sb_info *cifs_sb;
  1696. struct tcon_link *tlink;
  1697. struct cifs_tcon *tcon;
  1698. struct TCP_Server_Info *server;
  1699. char *full_path = NULL;
  1700. struct cifsInodeInfo *cifsInode;
  1701. cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
  1702. xid = get_xid();
  1703. full_path = build_path_from_dentry(direntry);
  1704. if (full_path == NULL) {
  1705. rc = -ENOMEM;
  1706. goto rmdir_exit;
  1707. }
  1708. cifs_sb = CIFS_SB(inode->i_sb);
  1709. tlink = cifs_sb_tlink(cifs_sb);
  1710. if (IS_ERR(tlink)) {
  1711. rc = PTR_ERR(tlink);
  1712. goto rmdir_exit;
  1713. }
  1714. tcon = tlink_tcon(tlink);
  1715. server = tcon->ses->server;
  1716. if (!server->ops->rmdir) {
  1717. rc = -ENOSYS;
  1718. cifs_put_tlink(tlink);
  1719. goto rmdir_exit;
  1720. }
  1721. if (tcon->nodelete) {
  1722. rc = -EACCES;
  1723. cifs_put_tlink(tlink);
  1724. goto rmdir_exit;
  1725. }
  1726. rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
  1727. cifs_put_tlink(tlink);
  1728. if (!rc) {
  1729. spin_lock(&d_inode(direntry)->i_lock);
  1730. i_size_write(d_inode(direntry), 0);
  1731. clear_nlink(d_inode(direntry));
  1732. spin_unlock(&d_inode(direntry)->i_lock);
  1733. }
  1734. cifsInode = CIFS_I(d_inode(direntry));
  1735. /* force revalidate to go get info when needed */
  1736. cifsInode->time = 0;
  1737. cifsInode = CIFS_I(inode);
  1738. /*
  1739. * Force revalidate to get parent dir info when needed since cached
  1740. * attributes are invalid now.
  1741. */
  1742. cifsInode->time = 0;
  1743. d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
  1744. current_time(inode);
  1745. rmdir_exit:
  1746. kfree(full_path);
  1747. free_xid(xid);
  1748. return rc;
  1749. }
  1750. static int
  1751. cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
  1752. const char *from_path, struct dentry *to_dentry,
  1753. const char *to_path)
  1754. {
  1755. struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
  1756. struct tcon_link *tlink;
  1757. struct cifs_tcon *tcon;
  1758. struct TCP_Server_Info *server;
  1759. struct cifs_fid fid;
  1760. struct cifs_open_parms oparms;
  1761. int oplock, rc;
  1762. tlink = cifs_sb_tlink(cifs_sb);
  1763. if (IS_ERR(tlink))
  1764. return PTR_ERR(tlink);
  1765. tcon = tlink_tcon(tlink);
  1766. server = tcon->ses->server;
  1767. if (!server->ops->rename)
  1768. return -ENOSYS;
  1769. /* try path-based rename first */
  1770. rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
  1771. /*
  1772. * Don't bother with rename by filehandle unless file is busy and
  1773. * source. Note that cross directory moves do not work with
  1774. * rename by filehandle to various Windows servers.
  1775. */
  1776. if (rc == 0 || rc != -EBUSY)
  1777. goto do_rename_exit;
  1778. /* Don't fall back to using SMB on SMB 2+ mount */
  1779. if (server->vals->protocol_id != 0)
  1780. goto do_rename_exit;
  1781. /* open-file renames don't work across directories */
  1782. if (to_dentry->d_parent != from_dentry->d_parent)
  1783. goto do_rename_exit;
  1784. oparms.tcon = tcon;
  1785. oparms.cifs_sb = cifs_sb;
  1786. /* open the file to be renamed -- we need DELETE perms */
  1787. oparms.desired_access = DELETE;
  1788. oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR);
  1789. oparms.disposition = FILE_OPEN;
  1790. oparms.path = from_path;
  1791. oparms.fid = &fid;
  1792. oparms.reconnect = false;
  1793. rc = CIFS_open(xid, &oparms, &oplock, NULL);
  1794. if (rc == 0) {
  1795. rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
  1796. (const char *) to_dentry->d_name.name,
  1797. cifs_sb->local_nls, cifs_remap(cifs_sb));
  1798. CIFSSMBClose(xid, tcon, fid.netfid);
  1799. }
  1800. do_rename_exit:
  1801. if (rc == 0)
  1802. d_move(from_dentry, to_dentry);
  1803. cifs_put_tlink(tlink);
  1804. return rc;
  1805. }
  1806. int
  1807. cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
  1808. struct inode *target_dir, struct dentry *target_dentry,
  1809. unsigned int flags)
  1810. {
  1811. char *from_name = NULL;
  1812. char *to_name = NULL;
  1813. struct cifs_sb_info *cifs_sb;
  1814. struct tcon_link *tlink;
  1815. struct cifs_tcon *tcon;
  1816. FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
  1817. FILE_UNIX_BASIC_INFO *info_buf_target;
  1818. unsigned int xid;
  1819. int rc, tmprc;
  1820. if (flags & ~RENAME_NOREPLACE)
  1821. return -EINVAL;
  1822. cifs_sb = CIFS_SB(source_dir->i_sb);
  1823. tlink = cifs_sb_tlink(cifs_sb);
  1824. if (IS_ERR(tlink))
  1825. return PTR_ERR(tlink);
  1826. tcon = tlink_tcon(tlink);
  1827. xid = get_xid();
  1828. /*
  1829. * we already have the rename sem so we do not need to
  1830. * grab it again here to protect the path integrity
  1831. */
  1832. from_name = build_path_from_dentry(source_dentry);
  1833. if (from_name == NULL) {
  1834. rc = -ENOMEM;
  1835. goto cifs_rename_exit;
  1836. }
  1837. to_name = build_path_from_dentry(target_dentry);
  1838. if (to_name == NULL) {
  1839. rc = -ENOMEM;
  1840. goto cifs_rename_exit;
  1841. }
  1842. rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
  1843. to_name);
  1844. /*
  1845. * No-replace is the natural behavior for CIFS, so skip unlink hacks.
  1846. */
  1847. if (flags & RENAME_NOREPLACE)
  1848. goto cifs_rename_exit;
  1849. if (rc == -EEXIST && tcon->unix_ext) {
  1850. /*
  1851. * Are src and dst hardlinks of same inode? We can only tell
  1852. * with unix extensions enabled.
  1853. */
  1854. info_buf_source =
  1855. kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
  1856. GFP_KERNEL);
  1857. if (info_buf_source == NULL) {
  1858. rc = -ENOMEM;
  1859. goto cifs_rename_exit;
  1860. }
  1861. info_buf_target = info_buf_source + 1;
  1862. tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
  1863. info_buf_source,
  1864. cifs_sb->local_nls,
  1865. cifs_remap(cifs_sb));
  1866. if (tmprc != 0)
  1867. goto unlink_target;
  1868. tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
  1869. info_buf_target,
  1870. cifs_sb->local_nls,
  1871. cifs_remap(cifs_sb));
  1872. if (tmprc == 0 && (info_buf_source->UniqueId ==
  1873. info_buf_target->UniqueId)) {
  1874. /* same file, POSIX says that this is a noop */
  1875. rc = 0;
  1876. goto cifs_rename_exit;
  1877. }
  1878. }
  1879. /*
  1880. * else ... BB we could add the same check for Windows by
  1881. * checking the UniqueId via FILE_INTERNAL_INFO
  1882. */
  1883. unlink_target:
  1884. /* Try unlinking the target dentry if it's not negative */
  1885. if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
  1886. if (d_is_dir(target_dentry))
  1887. tmprc = cifs_rmdir(target_dir, target_dentry);
  1888. else
  1889. tmprc = cifs_unlink(target_dir, target_dentry);
  1890. if (tmprc)
  1891. goto cifs_rename_exit;
  1892. rc = cifs_do_rename(xid, source_dentry, from_name,
  1893. target_dentry, to_name);
  1894. }
  1895. /* force revalidate to go get info when needed */
  1896. CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
  1897. source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
  1898. target_dir->i_mtime = current_time(source_dir);
  1899. cifs_rename_exit:
  1900. kfree(info_buf_source);
  1901. kfree(from_name);
  1902. kfree(to_name);
  1903. free_xid(xid);
  1904. cifs_put_tlink(tlink);
  1905. return rc;
  1906. }
  1907. static bool
  1908. cifs_inode_needs_reval(struct inode *inode)
  1909. {
  1910. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  1911. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  1912. if (cifs_i->time == 0)
  1913. return true;
  1914. if (CIFS_CACHE_READ(cifs_i))
  1915. return false;
  1916. if (!lookupCacheEnabled)
  1917. return true;
  1918. if (!cifs_sb->actimeo)
  1919. return true;
  1920. if (!time_in_range(jiffies, cifs_i->time,
  1921. cifs_i->time + cifs_sb->actimeo))
  1922. return true;
  1923. /* hardlinked files w/ noserverino get "special" treatment */
  1924. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
  1925. S_ISREG(inode->i_mode) && inode->i_nlink != 1)
  1926. return true;
  1927. return false;
  1928. }
  1929. /*
  1930. * Zap the cache. Called when invalid_mapping flag is set.
  1931. */
  1932. int
  1933. cifs_invalidate_mapping(struct inode *inode)
  1934. {
  1935. int rc = 0;
  1936. if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
  1937. rc = invalidate_inode_pages2(inode->i_mapping);
  1938. if (rc)
  1939. cifs_dbg(VFS, "%s: Could not invalidate inode %p\n",
  1940. __func__, inode);
  1941. }
  1942. cifs_fscache_reset_inode_cookie(inode);
  1943. return rc;
  1944. }
  1945. /**
  1946. * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
  1947. * @word: long word containing the bit lock
  1948. */
  1949. static int
  1950. cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
  1951. {
  1952. freezable_schedule_unsafe();
  1953. if (signal_pending_state(mode, current))
  1954. return -ERESTARTSYS;
  1955. return 0;
  1956. }
  1957. int
  1958. cifs_revalidate_mapping(struct inode *inode)
  1959. {
  1960. int rc;
  1961. unsigned long *flags = &CIFS_I(inode)->flags;
  1962. /* swapfiles are not supposed to be shared */
  1963. if (IS_SWAPFILE(inode))
  1964. return 0;
  1965. rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
  1966. TASK_KILLABLE);
  1967. if (rc)
  1968. return rc;
  1969. if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
  1970. rc = cifs_invalidate_mapping(inode);
  1971. if (rc)
  1972. set_bit(CIFS_INO_INVALID_MAPPING, flags);
  1973. }
  1974. clear_bit_unlock(CIFS_INO_LOCK, flags);
  1975. smp_mb__after_atomic();
  1976. wake_up_bit(flags, CIFS_INO_LOCK);
  1977. return rc;
  1978. }
  1979. int
  1980. cifs_zap_mapping(struct inode *inode)
  1981. {
  1982. set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
  1983. return cifs_revalidate_mapping(inode);
  1984. }
  1985. int cifs_revalidate_file_attr(struct file *filp)
  1986. {
  1987. int rc = 0;
  1988. struct inode *inode = file_inode(filp);
  1989. struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
  1990. if (!cifs_inode_needs_reval(inode))
  1991. return rc;
  1992. if (tlink_tcon(cfile->tlink)->unix_ext)
  1993. rc = cifs_get_file_info_unix(filp);
  1994. else
  1995. rc = cifs_get_file_info(filp);
  1996. return rc;
  1997. }
  1998. int cifs_revalidate_dentry_attr(struct dentry *dentry)
  1999. {
  2000. unsigned int xid;
  2001. int rc = 0;
  2002. struct inode *inode = d_inode(dentry);
  2003. struct super_block *sb = dentry->d_sb;
  2004. char *full_path = NULL;
  2005. int count = 0;
  2006. if (inode == NULL)
  2007. return -ENOENT;
  2008. if (!cifs_inode_needs_reval(inode))
  2009. return rc;
  2010. xid = get_xid();
  2011. /* can not safely grab the rename sem here if rename calls revalidate
  2012. since that would deadlock */
  2013. full_path = build_path_from_dentry(dentry);
  2014. if (full_path == NULL) {
  2015. rc = -ENOMEM;
  2016. goto out;
  2017. }
  2018. cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
  2019. full_path, inode, inode->i_count.counter,
  2020. dentry, cifs_get_time(dentry), jiffies);
  2021. again:
  2022. if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions)
  2023. rc = smb311_posix_get_inode_info(&inode, full_path, sb, xid);
  2024. else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
  2025. rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
  2026. else
  2027. rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
  2028. xid, NULL);
  2029. if (rc == -EAGAIN && count++ < 10)
  2030. goto again;
  2031. out:
  2032. kfree(full_path);
  2033. free_xid(xid);
  2034. return rc;
  2035. }
  2036. int cifs_revalidate_file(struct file *filp)
  2037. {
  2038. int rc;
  2039. struct inode *inode = file_inode(filp);
  2040. rc = cifs_revalidate_file_attr(filp);
  2041. if (rc)
  2042. return rc;
  2043. return cifs_revalidate_mapping(inode);
  2044. }
  2045. /* revalidate a dentry's inode attributes */
  2046. int cifs_revalidate_dentry(struct dentry *dentry)
  2047. {
  2048. int rc;
  2049. struct inode *inode = d_inode(dentry);
  2050. rc = cifs_revalidate_dentry_attr(dentry);
  2051. if (rc)
  2052. return rc;
  2053. return cifs_revalidate_mapping(inode);
  2054. }
  2055. int cifs_getattr(const struct path *path, struct kstat *stat,
  2056. u32 request_mask, unsigned int flags)
  2057. {
  2058. struct dentry *dentry = path->dentry;
  2059. struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
  2060. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  2061. struct inode *inode = d_inode(dentry);
  2062. int rc;
  2063. /*
  2064. * We need to be sure that all dirty pages are written and the server
  2065. * has actual ctime, mtime and file length.
  2066. */
  2067. if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
  2068. !CIFS_CACHE_READ(CIFS_I(inode)) &&
  2069. inode->i_mapping && inode->i_mapping->nrpages != 0) {
  2070. rc = filemap_fdatawait(inode->i_mapping);
  2071. if (rc) {
  2072. mapping_set_error(inode->i_mapping, rc);
  2073. return rc;
  2074. }
  2075. }
  2076. if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
  2077. CIFS_I(inode)->time = 0; /* force revalidate */
  2078. /*
  2079. * If the caller doesn't require syncing, only sync if
  2080. * necessary (e.g. due to earlier truncate or setattr
  2081. * invalidating the cached metadata)
  2082. */
  2083. if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
  2084. (CIFS_I(inode)->time == 0)) {
  2085. rc = cifs_revalidate_dentry_attr(dentry);
  2086. if (rc)
  2087. return rc;
  2088. }
  2089. generic_fillattr(inode, stat);
  2090. stat->blksize = cifs_sb->bsize;
  2091. stat->ino = CIFS_I(inode)->uniqueid;
  2092. /* old CIFS Unix Extensions doesn't return create time */
  2093. if (CIFS_I(inode)->createtime) {
  2094. stat->result_mask |= STATX_BTIME;
  2095. stat->btime =
  2096. cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
  2097. }
  2098. stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
  2099. if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
  2100. stat->attributes |= STATX_ATTR_COMPRESSED;
  2101. if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
  2102. stat->attributes |= STATX_ATTR_ENCRYPTED;
  2103. /*
  2104. * If on a multiuser mount without unix extensions or cifsacl being
  2105. * enabled, and the admin hasn't overridden them, set the ownership
  2106. * to the fsuid/fsgid of the current process.
  2107. */
  2108. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
  2109. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
  2110. !tcon->unix_ext) {
  2111. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
  2112. stat->uid = current_fsuid();
  2113. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
  2114. stat->gid = current_fsgid();
  2115. }
  2116. return 0;
  2117. }
  2118. int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
  2119. u64 len)
  2120. {
  2121. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  2122. struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->vfs_inode.i_sb);
  2123. struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
  2124. struct TCP_Server_Info *server = tcon->ses->server;
  2125. struct cifsFileInfo *cfile;
  2126. int rc;
  2127. /*
  2128. * We need to be sure that all dirty pages are written as they
  2129. * might fill holes on the server.
  2130. */
  2131. if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
  2132. inode->i_mapping->nrpages != 0) {
  2133. rc = filemap_fdatawait(inode->i_mapping);
  2134. if (rc) {
  2135. mapping_set_error(inode->i_mapping, rc);
  2136. return rc;
  2137. }
  2138. }
  2139. cfile = find_readable_file(cifs_i, false);
  2140. if (cfile == NULL)
  2141. return -EINVAL;
  2142. if (server->ops->fiemap) {
  2143. rc = server->ops->fiemap(tcon, cfile, fei, start, len);
  2144. cifsFileInfo_put(cfile);
  2145. return rc;
  2146. }
  2147. cifsFileInfo_put(cfile);
  2148. return -ENOTSUPP;
  2149. }
  2150. int cifs_truncate_page(struct address_space *mapping, loff_t from)
  2151. {
  2152. pgoff_t index = from >> PAGE_SHIFT;
  2153. unsigned offset = from & (PAGE_SIZE - 1);
  2154. struct page *page;
  2155. int rc = 0;
  2156. page = grab_cache_page(mapping, index);
  2157. if (!page)
  2158. return -ENOMEM;
  2159. zero_user_segment(page, offset, PAGE_SIZE);
  2160. unlock_page(page);
  2161. put_page(page);
  2162. return rc;
  2163. }
  2164. void cifs_setsize(struct inode *inode, loff_t offset)
  2165. {
  2166. struct cifsInodeInfo *cifs_i = CIFS_I(inode);
  2167. spin_lock(&inode->i_lock);
  2168. i_size_write(inode, offset);
  2169. spin_unlock(&inode->i_lock);
  2170. /* Cached inode must be refreshed on truncate */
  2171. cifs_i->time = 0;
  2172. truncate_pagecache(inode, offset);
  2173. }
  2174. static int
  2175. cifs_set_file_size(struct inode *inode, struct iattr *attrs,
  2176. unsigned int xid, char *full_path)
  2177. {
  2178. int rc;
  2179. struct cifsFileInfo *open_file;
  2180. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  2181. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2182. struct tcon_link *tlink = NULL;
  2183. struct cifs_tcon *tcon = NULL;
  2184. struct TCP_Server_Info *server;
  2185. /*
  2186. * To avoid spurious oplock breaks from server, in the case of
  2187. * inodes that we already have open, avoid doing path based
  2188. * setting of file size if we can do it by handle.
  2189. * This keeps our caching token (oplock) and avoids timeouts
  2190. * when the local oplock break takes longer to flush
  2191. * writebehind data than the SMB timeout for the SetPathInfo
  2192. * request would allow
  2193. */
  2194. open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
  2195. if (open_file) {
  2196. tcon = tlink_tcon(open_file->tlink);
  2197. server = tcon->ses->server;
  2198. if (server->ops->set_file_size)
  2199. rc = server->ops->set_file_size(xid, tcon, open_file,
  2200. attrs->ia_size, false);
  2201. else
  2202. rc = -ENOSYS;
  2203. cifsFileInfo_put(open_file);
  2204. cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
  2205. } else
  2206. rc = -EINVAL;
  2207. if (!rc)
  2208. goto set_size_out;
  2209. if (tcon == NULL) {
  2210. tlink = cifs_sb_tlink(cifs_sb);
  2211. if (IS_ERR(tlink))
  2212. return PTR_ERR(tlink);
  2213. tcon = tlink_tcon(tlink);
  2214. server = tcon->ses->server;
  2215. }
  2216. /*
  2217. * Set file size by pathname rather than by handle either because no
  2218. * valid, writeable file handle for it was found or because there was
  2219. * an error setting it by handle.
  2220. */
  2221. if (server->ops->set_path_size)
  2222. rc = server->ops->set_path_size(xid, tcon, full_path,
  2223. attrs->ia_size, cifs_sb, false);
  2224. else
  2225. rc = -ENOSYS;
  2226. cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
  2227. if (tlink)
  2228. cifs_put_tlink(tlink);
  2229. set_size_out:
  2230. if (rc == 0) {
  2231. cifsInode->server_eof = attrs->ia_size;
  2232. cifs_setsize(inode, attrs->ia_size);
  2233. /*
  2234. * i_blocks is not related to (i_size / i_blksize), but instead
  2235. * 512 byte (2**9) size is required for calculating num blocks.
  2236. * Until we can query the server for actual allocation size,
  2237. * this is best estimate we have for blocks allocated for a file
  2238. * Number of blocks must be rounded up so size 1 is not 0 blocks
  2239. */
  2240. inode->i_blocks = (512 - 1 + attrs->ia_size) >> 9;
  2241. /*
  2242. * The man page of truncate says if the size changed,
  2243. * then the st_ctime and st_mtime fields for the file
  2244. * are updated.
  2245. */
  2246. attrs->ia_ctime = attrs->ia_mtime = current_time(inode);
  2247. attrs->ia_valid |= ATTR_CTIME | ATTR_MTIME;
  2248. cifs_truncate_page(inode->i_mapping, inode->i_size);
  2249. }
  2250. return rc;
  2251. }
  2252. static int
  2253. cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
  2254. {
  2255. int rc;
  2256. unsigned int xid;
  2257. char *full_path = NULL;
  2258. struct inode *inode = d_inode(direntry);
  2259. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  2260. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2261. struct tcon_link *tlink;
  2262. struct cifs_tcon *pTcon;
  2263. struct cifs_unix_set_info_args *args = NULL;
  2264. struct cifsFileInfo *open_file;
  2265. cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
  2266. direntry, attrs->ia_valid);
  2267. xid = get_xid();
  2268. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  2269. attrs->ia_valid |= ATTR_FORCE;
  2270. rc = setattr_prepare(direntry, attrs);
  2271. if (rc < 0)
  2272. goto out;
  2273. full_path = build_path_from_dentry(direntry);
  2274. if (full_path == NULL) {
  2275. rc = -ENOMEM;
  2276. goto out;
  2277. }
  2278. /*
  2279. * Attempt to flush data before changing attributes. We need to do
  2280. * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
  2281. * ownership or mode then we may also need to do this. Here, we take
  2282. * the safe way out and just do the flush on all setattr requests. If
  2283. * the flush returns error, store it to report later and continue.
  2284. *
  2285. * BB: This should be smarter. Why bother flushing pages that
  2286. * will be truncated anyway? Also, should we error out here if
  2287. * the flush returns error?
  2288. */
  2289. rc = filemap_write_and_wait(inode->i_mapping);
  2290. if (is_interrupt_error(rc)) {
  2291. rc = -ERESTARTSYS;
  2292. goto out;
  2293. }
  2294. mapping_set_error(inode->i_mapping, rc);
  2295. rc = 0;
  2296. if (attrs->ia_valid & ATTR_SIZE) {
  2297. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  2298. if (rc != 0)
  2299. goto out;
  2300. }
  2301. /* skip mode change if it's just for clearing setuid/setgid */
  2302. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  2303. attrs->ia_valid &= ~ATTR_MODE;
  2304. args = kmalloc(sizeof(*args), GFP_KERNEL);
  2305. if (args == NULL) {
  2306. rc = -ENOMEM;
  2307. goto out;
  2308. }
  2309. /* set up the struct */
  2310. if (attrs->ia_valid & ATTR_MODE)
  2311. args->mode = attrs->ia_mode;
  2312. else
  2313. args->mode = NO_CHANGE_64;
  2314. if (attrs->ia_valid & ATTR_UID)
  2315. args->uid = attrs->ia_uid;
  2316. else
  2317. args->uid = INVALID_UID; /* no change */
  2318. if (attrs->ia_valid & ATTR_GID)
  2319. args->gid = attrs->ia_gid;
  2320. else
  2321. args->gid = INVALID_GID; /* no change */
  2322. if (attrs->ia_valid & ATTR_ATIME)
  2323. args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
  2324. else
  2325. args->atime = NO_CHANGE_64;
  2326. if (attrs->ia_valid & ATTR_MTIME)
  2327. args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
  2328. else
  2329. args->mtime = NO_CHANGE_64;
  2330. if (attrs->ia_valid & ATTR_CTIME)
  2331. args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
  2332. else
  2333. args->ctime = NO_CHANGE_64;
  2334. args->device = 0;
  2335. open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
  2336. if (open_file) {
  2337. u16 nfid = open_file->fid.netfid;
  2338. u32 npid = open_file->pid;
  2339. pTcon = tlink_tcon(open_file->tlink);
  2340. rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
  2341. cifsFileInfo_put(open_file);
  2342. } else {
  2343. tlink = cifs_sb_tlink(cifs_sb);
  2344. if (IS_ERR(tlink)) {
  2345. rc = PTR_ERR(tlink);
  2346. goto out;
  2347. }
  2348. pTcon = tlink_tcon(tlink);
  2349. rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
  2350. cifs_sb->local_nls,
  2351. cifs_remap(cifs_sb));
  2352. cifs_put_tlink(tlink);
  2353. }
  2354. if (rc)
  2355. goto out;
  2356. if ((attrs->ia_valid & ATTR_SIZE) &&
  2357. attrs->ia_size != i_size_read(inode))
  2358. truncate_setsize(inode, attrs->ia_size);
  2359. setattr_copy(inode, attrs);
  2360. mark_inode_dirty(inode);
  2361. /* force revalidate when any of these times are set since some
  2362. of the fs types (eg ext3, fat) do not have fine enough
  2363. time granularity to match protocol, and we do not have a
  2364. a way (yet) to query the server fs's time granularity (and
  2365. whether it rounds times down).
  2366. */
  2367. if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
  2368. cifsInode->time = 0;
  2369. out:
  2370. kfree(args);
  2371. kfree(full_path);
  2372. free_xid(xid);
  2373. return rc;
  2374. }
  2375. static int
  2376. cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
  2377. {
  2378. unsigned int xid;
  2379. kuid_t uid = INVALID_UID;
  2380. kgid_t gid = INVALID_GID;
  2381. struct inode *inode = d_inode(direntry);
  2382. struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
  2383. struct cifsInodeInfo *cifsInode = CIFS_I(inode);
  2384. struct cifsFileInfo *wfile;
  2385. struct cifs_tcon *tcon;
  2386. char *full_path = NULL;
  2387. int rc = -EACCES;
  2388. __u32 dosattr = 0;
  2389. __u64 mode = NO_CHANGE_64;
  2390. xid = get_xid();
  2391. cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
  2392. direntry, attrs->ia_valid);
  2393. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
  2394. attrs->ia_valid |= ATTR_FORCE;
  2395. rc = setattr_prepare(direntry, attrs);
  2396. if (rc < 0) {
  2397. free_xid(xid);
  2398. return rc;
  2399. }
  2400. full_path = build_path_from_dentry(direntry);
  2401. if (full_path == NULL) {
  2402. rc = -ENOMEM;
  2403. free_xid(xid);
  2404. return rc;
  2405. }
  2406. /*
  2407. * Attempt to flush data before changing attributes. We need to do
  2408. * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
  2409. * returns error, store it to report later and continue.
  2410. *
  2411. * BB: This should be smarter. Why bother flushing pages that
  2412. * will be truncated anyway? Also, should we error out here if
  2413. * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
  2414. */
  2415. if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
  2416. rc = filemap_write_and_wait(inode->i_mapping);
  2417. if (is_interrupt_error(rc)) {
  2418. rc = -ERESTARTSYS;
  2419. goto cifs_setattr_exit;
  2420. }
  2421. mapping_set_error(inode->i_mapping, rc);
  2422. }
  2423. rc = 0;
  2424. if ((attrs->ia_valid & ATTR_MTIME) &&
  2425. !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) {
  2426. rc = cifs_get_writable_file(cifsInode, FIND_WR_ANY, &wfile);
  2427. if (!rc) {
  2428. tcon = tlink_tcon(wfile->tlink);
  2429. rc = tcon->ses->server->ops->flush(xid, tcon, &wfile->fid);
  2430. cifsFileInfo_put(wfile);
  2431. if (rc)
  2432. goto cifs_setattr_exit;
  2433. } else if (rc != -EBADF)
  2434. goto cifs_setattr_exit;
  2435. else
  2436. rc = 0;
  2437. }
  2438. if (attrs->ia_valid & ATTR_SIZE) {
  2439. rc = cifs_set_file_size(inode, attrs, xid, full_path);
  2440. if (rc != 0)
  2441. goto cifs_setattr_exit;
  2442. }
  2443. if (attrs->ia_valid & ATTR_UID)
  2444. uid = attrs->ia_uid;
  2445. if (attrs->ia_valid & ATTR_GID)
  2446. gid = attrs->ia_gid;
  2447. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
  2448. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
  2449. if (uid_valid(uid) || gid_valid(gid)) {
  2450. rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
  2451. uid, gid);
  2452. if (rc) {
  2453. cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
  2454. __func__, rc);
  2455. goto cifs_setattr_exit;
  2456. }
  2457. }
  2458. } else
  2459. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
  2460. attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
  2461. /* skip mode change if it's just for clearing setuid/setgid */
  2462. if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
  2463. attrs->ia_valid &= ~ATTR_MODE;
  2464. if (attrs->ia_valid & ATTR_MODE) {
  2465. mode = attrs->ia_mode;
  2466. rc = 0;
  2467. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
  2468. (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
  2469. rc = id_mode_to_cifs_acl(inode, full_path, mode,
  2470. INVALID_UID, INVALID_GID);
  2471. if (rc) {
  2472. cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
  2473. __func__, rc);
  2474. goto cifs_setattr_exit;
  2475. }
  2476. } else
  2477. if (((mode & S_IWUGO) == 0) &&
  2478. (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
  2479. dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
  2480. /* fix up mode if we're not using dynperm */
  2481. if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
  2482. attrs->ia_mode = inode->i_mode & ~S_IWUGO;
  2483. } else if ((mode & S_IWUGO) &&
  2484. (cifsInode->cifsAttrs & ATTR_READONLY)) {
  2485. dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
  2486. /* Attributes of 0 are ignored */
  2487. if (dosattr == 0)
  2488. dosattr |= ATTR_NORMAL;
  2489. /* reset local inode permissions to normal */
  2490. if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  2491. attrs->ia_mode &= ~(S_IALLUGO);
  2492. if (S_ISDIR(inode->i_mode))
  2493. attrs->ia_mode |=
  2494. cifs_sb->mnt_dir_mode;
  2495. else
  2496. attrs->ia_mode |=
  2497. cifs_sb->mnt_file_mode;
  2498. }
  2499. } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
  2500. /* ignore mode change - ATTR_READONLY hasn't changed */
  2501. attrs->ia_valid &= ~ATTR_MODE;
  2502. }
  2503. }
  2504. if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
  2505. ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
  2506. rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
  2507. /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
  2508. /* Even if error on time set, no sense failing the call if
  2509. the server would set the time to a reasonable value anyway,
  2510. and this check ensures that we are not being called from
  2511. sys_utimes in which case we ought to fail the call back to
  2512. the user when the server rejects the call */
  2513. if ((rc) && (attrs->ia_valid &
  2514. (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
  2515. rc = 0;
  2516. }
  2517. /* do not need local check to inode_check_ok since the server does
  2518. that */
  2519. if (rc)
  2520. goto cifs_setattr_exit;
  2521. if ((attrs->ia_valid & ATTR_SIZE) &&
  2522. attrs->ia_size != i_size_read(inode))
  2523. truncate_setsize(inode, attrs->ia_size);
  2524. setattr_copy(inode, attrs);
  2525. mark_inode_dirty(inode);
  2526. cifs_setattr_exit:
  2527. kfree(full_path);
  2528. free_xid(xid);
  2529. return rc;
  2530. }
  2531. int
  2532. cifs_setattr(struct dentry *direntry, struct iattr *attrs)
  2533. {
  2534. struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
  2535. struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
  2536. int rc, retries = 0;
  2537. do {
  2538. if (pTcon->unix_ext)
  2539. rc = cifs_setattr_unix(direntry, attrs);
  2540. else
  2541. rc = cifs_setattr_nounix(direntry, attrs);
  2542. retries++;
  2543. } while (is_retryable_error(rc) && retries < 2);
  2544. /* BB: add cifs_setattr_legacy for really old servers */
  2545. return rc;
  2546. }
  2547. #if 0
  2548. void cifs_delete_inode(struct inode *inode)
  2549. {
  2550. cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
  2551. /* may have to add back in if and when safe distributed caching of
  2552. directories added e.g. via FindNotify */
  2553. }
  2554. #endif